Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

trouble trying to read a file and copy it to another....

 
william smythe
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working with this piece of code and it compiles fine but when i execute it says " exception in thread "main" Java.io.FileNotFoundException: ToBeCopied.txt (the system cannot find the file specified).....I have created a file with data called ToBeCopied.txt and an empty file called Copied.txt in the same directory as the code, what am i doing wrong???thanks again
import java.io.*;
public class Copy {
public static void main (String [] args) throws IOException {
File inputFile = new File("ToBeCopied.txt");
File outputFile = new File("Copied.txt");
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;
int count = 0;
while((c=in.read())!= -1){
System.out.print(c + " ");
if (count++%10==1)
System.out.println();
out.write(c);
}
in.close();
out.close();
}
}
 
deekasha gunwant
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi William,
I can't see any problem in your code.
moreover your code is running fine on my m/c.and is creating copied.txt file without any change.
so again make sure that u r having the file ToBeCopied.txt in place.

regards
deekasha
 
Michael.H.Sun
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William,
when I compiled, it gave a same error message as you received. How did you solve yours? Thanks.
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With the way your code is written, ToBeCopied.txt must be in the working directory for your JVM. You can determain this by
System.out.println(System.getProperty("user.dir"));
This is important if your are runnning this with some IDE's, it's not always the directory you think.

------------------
Hope This Helps
Carl Trusiak
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!