By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
463,060 Members | 544 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 463,060 IT Pros & Developers. It's quick & easy.

How to read and write files using java?

P: 61
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.FileOutputStream
FileOutputStream fout = new FileOutputStream(outputFile);
^
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3.   public class Copy {
  4.       public static void main(String[] args) throws IOException {
  5.           // create two file references
  6.           File inputFile = new File("D:/1.txt");
  7.           File outputFile = new File("D:/2.txt");
  8.  
  9.           // create two File Streams
  10.           FileReader in = new FileReader(inputFile);
  11.           FileWriter out = new FileWriter(outputFile);
  12.  
  13.           // read one file and write it out to another
  14.           int c;
  15.  
  16.           while ((c = in.read()) != -1)
  17.              out.write(c);
  18.  
  19.           // close both files
  20.           in.close();
  21.           out.close();
  22.       }
  23.   }
What i have to do for this?

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


senthil.
Feb 7 '07 #1
Share this Question
Share on Google+
2 Replies

Expert 100+
P: 1,510
the program you posted compiles OK - what is your problem?
Feb 7 '07 #2

10K+
P: 13,262
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.FileOutputStream
FileOutputStream fout = new FileOutputStream(outputFile);
^
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3. public class Copy {
  4. public static void main(String[] args) throws IOException {
  5. // create two file references
  6. File inputFile = new File("D:/1.txt");
  7. File outputFile = new File("D:/2.txt");
  8.  
  9. // create two File Streams
  10. FileReader in = new FileReader(inputFile);
  11. FileWriter out = new FileWriter(outputFile);
  12.  
  13. // read one file and write it out to another
  14. int c;
  15.  
  16. while ((c = in.read()) != -1)
  17. out.write(c);
  18.  
  19. // close both files
  20. in.close();
  21. out.close();
  22. }
  23. }
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.
Feb 7 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.