Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
William,
when I compiled, it gave a same error message as you received. How did you solve yours? Thanks.
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ew. You guys are ugly with a capital UG. Here, maybe this tiny ad can help:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic