This week's book giveaway is in the Web Services forum.
We're giving away four copies of Microservices in Action and have Morgan Bruce & Paulo A. Pereira on-line!
See this thread for details.
Win a copy of Microservices in Action this week in the Web Services 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

File Reading Error  RSS feed

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class file with the following code :
------------------------
import java.io.*;
public class Copy {
public static void main(String[] args) throws IOException {
File inputFile = new File("farrago.txt");
File outputFile = new File("outagain.txt");
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;
while ((c = in.read()) != -1)
out.write(c);
in.close();
out.close();
}
}
-----------------------------------
The farrago.txt file is in the same folder as is the above Copy.class file.
When i try to run this program, it gives me the following error :
Exception in thread "main" java.io.FileNotFoundException: farrago.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:68)
at java.io.FileInputStream.<init>(FileInputStream.java:99)
at java.io.FileReader.<init>(FileReader.java:39)
at Copy.main(Copy.java, Compiled Code)
Please help.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wish I had the time to give you the reasoning, but I'm very busy. In the meantime;
Try explicitly using the complete filename.
"c:\datafiles\farrago.txt"
 
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajeev,
I just tried your code on my machine and it worked perfectly! I just made a simple text file using Notepad and named it "farrago.txt". This makes me suspect there is something wrong with your "farrago.txt" file. For example, is the spelling of the file name on disk the same as in your program?
Let me know.
Stephanie
 
rajeev holkar
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The spelling of the file is OK.
I have set the classpath to the folder which contains Copy.java,Copy.class and farrago.txt file.
Is there anything to do with this?
I tried using the complete file name as suggested by Apu but it is giving me four 'invalid escape character errors'.
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Speaking Java" it should be: "c:\\datafiles\\farrago.txt".
 
rajeev holkar
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'myfolder' contains all .class and .java and .txt files.
The classpath set is C:\myfolder.Is this correct? or is it have to be like this C:\myfolder\ (with one more backslash in last.)
Is the classpath set in quotes("C:\myfolder").
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is you aren't in the same directory as the farrago.txt file. Since your classpath includes the directory that has Copy.class java can find it. either change to that diectory to execute Copy.class or use the fully qualified path name to the file.
"c:\\datafiles\\farrago.txt" as suggested by Apu (only remember that \ is the escape character in java and to get a \ in a String you have to use 2 or \\)
Hope this helps
 
Michal Harezlak
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajeev:
The classpath is most likely OK. The stream can not find your file , that is why you see: java.io.FileNotFoundException. I know it is frustrating, but that is where I would look for the problem. What platform are you on?
 
rajeev holkar
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey I got it! The thing is working now.
thanks Michal and all others for help.
i checked my classpath and it is C:\myfolder
But two things i do not understand.
1. The same thing was running on Stephanie's machine.
2. My outagain.txt file was created in C root when i expected it to be in the same folder which contains my class file.
 
rajeev holkar
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Carl and Michal.
Actually, the \\ thing did the trick.
And being new, i didn,t knew that.
Thanks a lot.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!