Simple Program for Reading Writing Content From File

Program for reading content from file:

import java.io.*;
public class MyFirstFilereadingApp
{
 // Main method
 public static void main (String args[])
 {
  // Stream to read file
  FileInputStream fin;
  try
  {
      // Open an input stream
      fin = new FileInputStream ("c:\\myfile1.txt");
      // Read a line of text
      System.out.println( new DataInputStream(fin).readLine() );
      // Close our input stream
      fin.close();
  }
  // Catches any error conditions
  catch (IOException e)
  {
   System.err.println ("Unable to read fr! om file");
   System.exit(-1);
  }
}
}

Program for writing content from file:

import java.io.*;
public class MyFirstFileWritingApp
{
 // Main method
 public static void main (String args[])
 {
  // Stream to write file
  FileOutputStream fout;
  try
  {
      // Open an output stream
      fout = new FileOutputStream ("c:\\myfile1.txt");
      // Print a line of text
      new PrintStream(fout).println ("hello world!");
      // Close our output stream
      fout.close();
  }
  // Catches any error conditions
  catch (IOException e)
  {
   System.err.println ("Unable to write to file");
   System.exit(-1);
  }
 }
}

Java Tips by : Ilango Van

Related:

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.