211091

Java-FileWriter

FileWriter

FileWriter class is an important subclass of the Writer class. FileWriter class provides the functionality of writing characters into a file. In the following program, an object is created for FileWriter class by passing the data file name output.txt as the argument for its constructor. Then the alphabets are written in this data file, by invoking the write( ) method of FileWriter class. Finally, the file output.txt is closed.

FileWriter Constructors

Constructors Description
FileWriter(File file) Creates a FileWriter object given a File object.
FileWriter(File file, boolean append) Creates a FileWriter object given a File object with a boolean indicating whether or not to append the data written.
FileWriter(String fileName) Creates a FileWriter object given a file name.
FileWriter(String fileName, boolean append) Creates a FileWriter object given a file name with a boolean indicating whether or not to append the data written.

FileWriter Methods

Method Description
void write(int c) This method used to write the single character.
void write(String str) This method used to write the string.
void write(String str, int off, int len) This method used to write the portion of a string.
void write(char[] cbuf, int off, int len) This method used to write the portion of an array of characters.
FileWriter append(char c) This method appends the specified character to this writer.
FileWriter append(CharSequence csq, int start, int end) This method appends a subsequence of the specified character sequence to this writer.
void flush() This method used to flush the stream.
void close() This method used to close the stream.

Closing a File

When you are done with a file, you must close it. This is done by calling the close( ) method. Closing a file releases the system resources allocated to the file, allowing them to be used by another file. Failure to close a file can result in “memory leaks” because of unused resources remaining allocated.

Input / Output Exceptions

When creating files and performing input/output operations on them, the system may generate input/output related exceptions. Each input/output statement or group of input/output statements must have an exception handler around it or the method must declare that it throws an IOException. Proper use of exception handlers would help us identify and locate input/output errors more effectively.

Program

FILEWRITER

Program Source

import java.io.FileWriter;
import java.io.IOException;

class Javaapp {
    
    public static void main(String[] args)  {
        
        FileWriter fw = null;
        
        try{
            fw = new FileWriter("output.txt");
            fw.write("A1B2C3D4E5F6");
            
        }catch(IOException io)
        {
            System.out.println("Exception : "+io);
            
        }finally{
            
            try{
                fw.close();
                
            }catch(IOException io)
            {
                System.out.println("Exception : "+io);
            }
        }
    }
}

Leave a Comment