Menu
Topics Index
...
`


Input/Output: Exploring java.io > Character Streams >
Siva Nookala - 14 Apr 2016
Convenience class for writing character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable. To specify these values yourself, construct an OutputStreamWriter on a FileOutputStream.

Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileWriter(or other file-writing object) at a time. In such situations the constructors in this class will fail if the file involved is already open.

FileWriter is meant for writing streams of characters. For writing streams of raw bytes, consider using a Java FileOutputStream.

FileWriter Constructors:
ConstructorDescription
FileWriter(File file)Constructs a FileWriterobject given a File object.
FileWriter(File file, boolean append)Constructs a FileWriter object given a File object.
FileWriter(FileDescriptor fd)Constructs a FileWriter object associated with a file descriptor.
FileWriter(String fileName)Constructs a FileWriterobject associated with a file descriptor.
FileWriter(String fileName, boolean append)Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written.

Once you have FileWriter object in hand, then there is a list of helper methods, which can be used manipulate the files.

FileWriter Methods:
MethodDescription
void write(int c) throws IOExceptionWrites a single character.
void write(char [] c, int offset, int len)Writes a portion of an array of characters starting from offset and with a length of len.
void write(String s, int offset, int len)Write a portion of a String starting from offsetand with a length of len.

Example Program:
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileReaderWriter{

    public static void main(String args[]) throws IOException{

        File file = new File("Output.txt"); //LINE A

        // creates the file
        file.createNewFile();

        // creates a FileWriter Object
        FileWriter fw = new FileWriter(file);

        // writes the content to the file
        fw.write("This\nis\nJava\nMerit\nCampus."); // LINE B
        fw.flush();
        fw.close();

        // creates a FileReader Object
        FileReader fr = new FileReader(file);
        char [] output = new char[50];

        fr.read(output); // reads the content to the array

        for(char out : output)
        {
            System.out.print(out); // prints the characters one by one
        }

        fr.close();
    }
}

Output:
This
is
Java
Merit
Campus.

Description:
A text file Output.txt is taken and created. Input is taken into the FileWriter and is written onto the file. The file is read using FileReader and printed.

Things to try:
  • Try giving other inputs to File in LINE A.
  • Try giving other inputs to FileWriter in LINE B like "Your Online Java School"<cw>, "Don't just learn Java, do Java".

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.