Advertisement
andylai

FileRead

Aug 29th, 2014
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.71 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. public class FileRead{
  4.  
  5.    public static void main(String args[])throws IOException{
  6.  
  7.       File file = new File("Hello1.txt");
  8.       // creates the file
  9.       file.createNewFile();
  10.       // creates a FileWriter Object
  11.       FileWriter writer = new FileWriter(file);
  12.       // Writes the content to the file
  13.       writer.write("This\n is\n an\n example\n");
  14.       writer.flush();
  15.       writer.close();
  16.  
  17.       //Creates a FileReader Object
  18.       FileReader fr = new FileReader(file);
  19.       char [] a = new char[50];
  20.       fr.read(a); // reads the content to the array
  21.       for(char c : a)
  22.           System.out.print(c); //prints the characters one by one
  23.       fr.close();
  24.    }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement