Updated: 01/02/2024 by Computer Hope

Printwriter vs Filewriter class in java with example

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 :

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");
    		}	
    	}
    }
}
}

Second example to writing details of object like name , phone number using FileWriter

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 :

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.

Third example to print details like name , phone number store in file using PrintWriter

Here we are Showing a detail using print() method

print() method is applicable for all data type
don'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!");
    		
    			
    	}
    }
}
}

Conclusion

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.