Java Demo, Reading and Writing with Files

Notes:


import java.io.*;   // Import the Input and Output Stream Classes

public class JavaDemo{
   public static void main(String[] args){
      try{
         BufferedReader infile =
                new BufferedReader(new FileReader("sampleFile.txt"));	  
                                               // Open for reading
         System.out.println("File open for reading...");
         BufferedWriter outfile = 
                   new BufferedWriter(new FileWriter("writefile.txt"));	
                               // Open for writing
         System.out.println("File open for writing...");
         String line;  
         while((line = infile.readLine()) != null)
         {
            System.out.println(line);	// routes output to CRT
            outfile.write(line); //writes to a file
         //Or use the following syntax to write a portion of a String: 
         //   outfile.write(line, 0, line.length());  // routes output to file
          
            outfile.newLine();    //generate a newline in the outfile
         }
         System.out.println("Done copying file...");
         infile.close();
         outfile.close();
         System.out.println("Files closed.");
      }
      catch(IOException e){
            System.err.println(e);
            return;
}}}