Search This Blog

Friday 12 September 2014

Read whole file into character array in Java - File Handling - java

How to Read entire file into character array in Java - File Handling in java

FileReader's read(char []) function allows you to load entire file into the character array.

This program shows how to Read an entire file into character array in Java..

Testing_.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Testing_
{
    public static void main(String args[]) throws FileNotFoundException{
     try {
   FileReader fileReader = new FileReader(new File("myFile.txt"));
   char [] buff = new char[1000];
   fileReader.read(buff);
   System.out.print(buff);
   fileReader.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
    }
}

No comments:

Post a Comment