Showing posts with label java file handling. Show all posts
Showing posts with label java file handling. Show all posts

Saturday, February 18, 2012

Java File Handling Program - How to Handle File in Java

It is a java example to demonstrate how to handle any File using Java programming. In this program open a file and show the complete data of file as a output.

File Name - "FILE_MAIN.java"
Output of this program is as same as compiled program. It's open program file and print complete program copy as a output.


Program Code
//program output is program itself which compiled
import java.util.*;
import java.io.*;
public class FILE_MAIN
{  
 public static void main(String[] args) throws FileNotFoundException
 { 
  FileReader dataFile = new FileReader("FILE_MAIN.java");
  Scanner FileRead = new Scanner(dataFile);
  while (FileRead.hasNextLine())
  {
   String Line1 = FileRead.nextLine();
   System.out.println(Line1);
  }
  FileRead.close();
 }
}

Output
Save file name as "FILE_MAIN.java".


It is a example of File Handling. In this program output is same copy of program itself.

Hope, it's helpful to understanding Java file handling.