View RSS Feed

Eclipse IDE

File I/O in eclipse

Rate this Entry
by , 02-11-2012 at 06:38 PM (1344 Views)
In this section, we will explain the File I/O process in which your program will read from a text file and write to a text file. Following steps explains this process.

Create a new Java Program in your Eclipse.
Write the following lines of code into your Java Program.
Compile and run the Java Program.
It will read from a file and write it to another file.

Java Code: This is the code to read and write a file
import java.io.*;
class FileRead 
{
 public static void main(String args[])
  {
  try{
  // Open the file that is the first 
  // command line parameter
  FileInputStream fstream = new FileInputStream("textfile.txt");
  // Get the object of DataInputStream
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine;
  //Read File Line By Line
  while ((strLine = br.readLine()) != null)   {
  // Print the content on the console
  System.out.println (strLine);
  }
  //Close the input stream
  in.close();
    }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
  }
}

Submit "File I/O in eclipse" to Facebook Submit "File I/O in eclipse" to Digg Submit "File I/O in eclipse" to del.icio.us Submit "File I/O in eclipse" to StumbleUpon Submit "File I/O in eclipse" to Google

Categories
Eclipse Files

Comments

  1. fatabass's Avatar
    • |
    • permalink
    What does this have to do anything with Eclipse ?

    This is just a standard Java code.