How to read and write files using java?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • asenthil
    New Member
    • Dec 2006
    • 61

    #1

    How to read and write files using java?

    i'm trying to read and write files using java...

    some errors occurs when i'm trying this code..

    Error in java: Cannot find symbol
    location: class java.io.FileOut putStream
    FileOutputStrea m fout = new FileOutputStrea m(outputFile);
    ^
    Code:
    import java.io.*;
    
      public class Copy {
          public static void main(String[] args) throws IOException {
              // create two file references
              File inputFile = new File("D:/1.txt");
              File outputFile = new File("D:/2.txt");
    
              // create two File Streams
              FileReader in = new FileReader(inputFile);
              FileWriter out = new FileWriter(outputFile);
    
              // read one file and write it out to another
              int c;
    
              while ((c = in.read()) != -1)
                 out.write(c);
    
              // close both files
              in.close();
              out.close();
          }
      }
    What i have to do for this?

    now only i'm learning java...
    thats y...


    senthil.
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    the program you posted compiles OK - what is your problem?

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by asenthil
      i'm trying to read and write files using java...

      some errors occurs when i'm trying this code..

      Error in java: Cannot find symbol
      location: class java.io.FileOut putStream
      FileOutputStrea m fout = new FileOutputStrea m(outputFile);
      ^
      Code:
      import java.io.*;
       
      public class Copy {
      public static void main(String[] args) throws IOException {
      // create two file references
      File inputFile = new File("D:/1.txt");
      File outputFile = new File("D:/2.txt");
       
      // create two File Streams
      FileReader in = new FileReader(inputFile);
      FileWriter out = new FileWriter(outputFile);
       
      // read one file and write it out to another
      int c;
       
      while ((c = in.read()) != -1)
      out.write(c);
       
      // close both files
      in.close();
      out.close();
      }
      }
      What i have to do for this?

      now only i'm learning java...
      thats y...


      senthil.
      This code is not the one that produced the error you posted. Check again.

      May I add that you should be using a bufferedReader and BufferedWriter to wrap the FileReader and FileWriters resp.

      Comment

      Working...