Author Topic: Getting Introduced to FileWriter and FileReader in Java  (Read 722 times)

jitudas

  • Sr. Member
  • ****
  • Posts: 423
  • If You Believe You Can, Than You Should Do It.
Getting Introduced to FileWriter and FileReader in Java
« on: April 26, 2015, 03:05:38 PM »

Java provides the class FileWriter to write int, byte array and string into a file. FileReader class is used for reading streams of characters.

Before looking into FileWriter and FileReader, let's see what is a file.
If we want to store data permanently then we will store that data in files. File class is used to create a file, check file existence etc. let's write a sample code that will create a file and checks whether the file is created or not. For creating a new file File.createNewFile( ) method is used. This method returns a boolean value true if the file is created otherwise returns false. If the mentioned file for the specified directory is already exist then the createNewFile() method returns the false otherwise the method creates the mentioned file and return true.

import java.io.*;
class FileCreation{
  public static void main(String[] args) throws IOException{
  File f;
  f=new File("newFile.txt");
  if(!f.exists()){
  f.createNewFile();
  System.out.println("New file is created to the current directory");
  }
  }
}


In the above program we first passed the file name as constructor argument, at that point the file will not be created. After calling the method createNewFile() the file will be created. exists() method will check whether the file with same name in the directory is present or not , If there is a file already with same name then it will return false else it will return true. All most all the I/O classes may throw IOException so we need to handle by writing the code in try/catch blocks or declare the method as throwing IOException.

FileWriter is used to write data into a file in java, it provides overloaded write method to write int, byte array and String to the File. We can also write part of the String or byte array using FileWriter. FileWriter writes directly into Files and should be used only when number of writes are less. FileReader class inherits from the InputStreamReader class. FileReader is used for reading streams of characters. A good concept as per sun certified java programmer find out more  point of view. Let's write a program that will create a file, writes data into it and reads it.

Creation and writing data intoFile:
1.Creation of file
File file = new File( "newfile.txt");
   FileWriter f1 =new FileWriter(file);

2.Writing data into file
        f1.write("whizlabs\nsimulator\n");

3.Flushing and closing the stream
        f1.flush();
        f1.close();

Reading data from file:
1.Open the file using FileReader
FileReader f2=new FileReader(file);

2.Reading data and printing
char[] data = new char[50];
for(char c : data)
System.out.print(c);

3.Closing the file stream
f2.close();

Let's combine the above steps and write a program for creating a file, writing some data into it , reading that data from that file and printing it.
import java.util.*;
import java.io.*;
public class Package {
  public static void main(String args[])throws Exception
    {       
                try
                {                   
                    File file = new File( "newfile.txt");
                    FileWriter fw =new FileWriter(file);    //creates file with name newfile
                    fw.write("whizlabs\nsimulators\n");  // writes data into the file
                    fw.flush();      // flush before closing
                    fw.close();      // closing the resource
                    FileReader fr =new FileReader(file);    //opening the file
                    char[] in = new char[25];
                     fr.read(in); // read the whole file into array
                    for(char c : in) // print the array
                    System.out.print(c);
                    fr.close();
                } catch(IOException e) { }
    }
This is a good topic covered in many good sun java tutorial  Whizlabs
Jitu Das
Deploma in CSE
Students
Bangladesh Skill Development Institute
Contact Number:+8801620210620