Updated: 01/02/2024 by Computer Hope
Characters can be written to a file in Java using the PrintWriter and FileWriter classes. They differ somewhat in terms of functionality and application, though. Let's talk about each class and give illustrations for each.
FileWriter class of java.io package is used to write data in character form to file. FileWriter class inherits from OutputStreamWriter class which in turn inherits from the Writer class. FileWriter is meant for writing streams of characters in file. It is a character-oriented class.
Below is example of FileWriter class
import java.io.*;
import java.util.*;
class FileExample4
{
public static void main(String args[])
{
FileWriter fw=null;
try
{
fw=new FileWriter("D:/Assignments/message.txt");
Scanner kb=new Scanner(System.in);
System.out.println("Type a message");
String str=kb.nextLine();
fw.write(str);
}
}
catch(IOException ex)
{
System.out.println("Error while writing file");
}
finally
{
if(fw!=null)
{
try
{
fw.close();
System.out.println("File saved successfully!");
}
catch(IOException ex)
{
System.out.println("Error while closing file");
}
}
}
}
}
Here we are writing defferent data type in file using FileWriter class
import java.io.*;
import java.util.*;
class FileExample4
{
public static void main(String args[])
{
FileWriter fw=null;
try
{
fw=new FileWriter("D:/Assignments/myfriends.txt");
Scanner kb=new Scanner(System.in);
for(int i=0;i<2;i++)
{
System.out.println("Enter name and phone");
String name=kb.next();
long phone=kb.nextLong();
fw.write(name);
fw.write(String.valueOf(phone));
fw.write("\r\n");
}
}
catch(IOException ex)
{
System.out.println("Error while writing file");
}
finally
{
if(fw!=null)
{
try
{
fw.close();
System.out.println("File saved successfully!");
}
catch(IOException ex)
{
System.out.println("Error while closing file");
}
}
}
}
}
PrintWriter is a class in Java that is a subclass of Writer and provides higher-level methods for formatted text output. It can be used with various data types and has convenient methods like println for printing a line of text. PrintWriter of java.io package is used to write data in character form to file.
Here we are Showing a detail using print() methodprint() method is applicable for all data typedon't forget to close objectin end using pw.close() method.
import java.io.*;
import java.util.*;
class FileExample4
{
public static void main(String args[])
{
PrintWriter pw=null;
try
{
pw=new PrintWriter("D:/Assignments/myfriends.txt");
Scanner kb=new Scanner(System.in);
for(int i=0;i<2;i++)
{
System.out.println("Enter name and phone");
String name=kb.next();
long phone=kb.nextLong();
pw.print(name+" ");
pw.println(phone);
}
}
catch(IOException ex)
{
System.out.println("Error while writing file");
}
finally
{
if(pw!=null)
{
pw.close();
System.out.println("File saved successfully!");
}
}
}
}
In this article,FileWriter is a lower-level class for writing character data to a file. In general, if you need more advanced text output capabilities and convenient methods for writing different types of data, PrintWriter is a good choice. If you just need to write raw text, FileWriter might be sufficient.