File Handling in Java – Java FileReader & FileWriter

1. Objective

After studying, Singleton Class in Java, we are ready to discuss File Handling in Java. Moreover, we will discuss Java Filewriter and Java File reader with their constructors and methods used.

So, let’s start with File Handling in Java.

File Handling in Java - Java FileReader & FileWriter

File Handling in Java – Java FileReader & FileWriter

2. What is File Handling in Java?

File Handling in Java programming Language: FileWriter and FileReader classes are very frequently used to write and read data from text files (they are Character Stream classes). For any Byte stream classes, if you want to read and write them it is not wise and recommended to use FileInputStream.

Do you know What is Java.io.File Class?

3. Java FileWriter

Java FileWriter is very useful in creating a file writing characters.

  • This class inherits from the OutputStream class.
  • The constructors of the class FileWriter usually assume that the byte-buffer size and default character encoding is acceptable.
  • To declare them by oneself we need to construct OutputStreamWriter on a FileOutputStream.
  • Java FileWriter is meant for writing streams of characters.

a. Constructors

  • FileWriter(File file) – This constructor constructs a FileWriter object when a file object is given.
  • FileWriter (File file, boolean append) – This constructor constructs a FileWriter object.
  • FileWriter (FileDescriptor fd) – This constructor constructs a FileWriter object associated using a file descriptor.
  • FileWriter (String fileName) –This constructor constructs a FileWriter object when a file name is given.
  • FileWriter (String fileName, Boolean append) – This constructor constructs a FileWriter object when a file name is given with a Boolean to decide whether it append or not.
  • public void write (int c) throws IOException – This constructor writes a single character.
  • public void write (char [] stir) throws IOException – This constructor writes an array of characters.
  • public void write(String str)throws IOException – This constructor writes a string.
  • public void write(String str, int off, int len)throws IOException – This constructor writes a portion of a string.

Reading and writing are done in an accordance that is character by character, which increases the count of I/O operations and effects performance of the system. BufferedWriter can use with FileWriter to improve the speed of execution of the operation.

Follow this link to know about Inheritance in Java

Following program depicts how to create a text file using Java FileWriter

import java.io.FileWriter;
import java.io.IOException;
class CreateFile
{
    public static void main(String[] args) throws IOException
    {
        String str = "File Handling in Java using "+
                     " FileWriter and FileReader";
        FileWriter fw=new FileWriter("text");
        for (int k = 0; k< str.length(); k++)
            fw.write(str.charAt(i));
        fw.close();
    }
}

4. Java FileReader

Java FileReader uses for reading the data which are in the form of characters and it is done from a ‘text’ file.

This class inherits from the InputStreamReader Class.

The constructors of this class are assuming that the default character encoding and the default byte- are appropriate. To confirm these values by your own, construct an InputStreamReader on a FileInputStream.

Java FileReader uses for particularly reading streams of character. For reading streams of raw bytes, FileInputStream can use.

Follow this link to know about Collection Framework in Java

a. Constructors

  • FileReader(File file) – This constructor creates a FileReader only when there is File to read from.
  • FileReader(FileDescripter fd) – This constructor creates a new FileReader when there is a FileDescripter from which it can read from.
  • FileReader(String fileName) – This constructor creates a new FileReader.

b. Methods

  • public int read () throws IOException – This method reads a single character and also blocks one until another one is available, i.e. an input/output error occurs.
  • public int read(char[] cbuff) throws IOException – This method reads characters into an array. It will block until a character is available.
  • public abstract int read(char[] buff, int off, int len) which throws an IOException – This method use to read characters into a portion of an array. It will block the process until the input is available or an error occurs in input and output, or the stream end reach.

Parameters:

cbuf – Destination buffer.
off – This method use to set an offset at which to start storing characters.
len – This method uses to see a maximum number of characters to read.

  • public long skip(long n) throws IOException –This method skips characters and also blocks until some characters are available, an I/O error occurs, or the end of the stream reach.

Let’s explore Command Line Arguments In Java

Following program depicts how to read from the ‘text’ file using FileReader

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
class ReadFile
{
    public static void main(String[] args) throws IOException
    {
        int ch;
        FileReader fr=null;
        try
        {
            fr = new FileReader("text");
        }
        catch (FileNotFoundException fe)
        {
            System.out.println("File not found");
        }
               while ((ch=fr.read())!=-1)
            System.out.print((char)ch);
        fr.close();
    }
}

This was all about File Handling in Java Tutorial. Hope you like our explanation.

5. Conclusion

Hence, in this Java tutorial, we learned what is file handling in Java. In addition, we discussed File Handling in java with example and Java Filewriter & Java Filereder with their example. Furthermore, if you have any doubt feel free to ask in the comment box.

Related Topic- 3 Ways To Read Java Input

For reference 

1 Response

  1. Bhaiyyaji Sonawane says:

    import java.io.FileWriter;
    import java.io.IOException;
    class CreateFile
    {
    public static void main(String[] args) throws IOException
    {
    String str = “File Handling in Java using “+
    ” FileWriter and FileReader”;
    FileWriter fw=new FileWriter(“text”);
    for (int k = 0; k>>>>> fw.write(str.charAt(i));
    fw.close();
    }
    }

    Errorias code… pleas fix it

Leave a Reply

Your email address will not be published. Required fields are marked *