• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why can't i copy txt files?

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay so heres the code I am using, if you havent realised i had been testing the code from the oracle java tutorial website:



and heres the error message i keep getting:

Exception in thread "main" java.io.FileNotFoundException: xanudu.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at package1.ScanXan2.main(ScanXan2.java:14)


I just don't understand, I have made the xanudu file in the correct java directory (the file has also got text in it) because i can see it on the eclipse sidebar along with all my other java files.

Any advice at all would be appreciated
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try printing out the result of new File("xanadu.txt").getAbsolutePath(). That may not be what you think it should be.
 
Adamz Preston
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the result after I put in a catch statement:

C:\Documents and Settings\MOTIVE\My Documents\eclipse\workspace\Java language\xanudu.txt

But I can see that my txt file is:
C:\Documents and Settings\MOTIVE\My Documents\eclipse\workspace\Java language\src\package1\xanudu.txt

This was done by going onto the system properties of the txt file.

I also just tried refreshing the eclipse sidebar and the same result still came.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the file isn't where your code expects it to be; how can you fix that? Note that using relative paths -like your code does- is bound to work differently if you run your code inside of an IDE as compared to running it standalone (w/o an IDE).
 
Adamz Preston
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically, your trying to tell me to find the file using a path instead of relying on where the application is based to find it. Well after looking through the java tutorial list this is mentioned and I will get to this shortly. But I will have to find out how to get file reading from the program location done eventually, otherwise my apps may not be compatible with other systems.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there are 2 basic rules that you need to remember here

1) if you give a Relative path, java will look for the file relative to the Current directory. This is usually the location where the user is running the application, however it can be changed programitaclly.
2) if you provide an Absolute path, java will use the absolute path


Generally, there are 2 ways most apps open files
A) user provides the file path. In this case the user either provides a relative or absolute path, and the app uses the path. Here, the user is responsible for giving the correct path to the app
B) the path is used to read a configuration file that is bundled with the app. In this case the user doesn't know what the path is. The application knows the relative path to the file, but doesn't know the absolute path. Usually what is done in this case is that user is responsible for setting an environment variable that point s to the root of the installation folder, and the application adds the relative path to the root path to get the absolute path to the file.

For example, when you install java itself, you set the environment variable JAVA_HOME. This points to the root folder where your java is installed. Java itself knows where the files it needs are relative to the root path, and it uses the Envvar to find the absolute path of the files it needs
 
Adamz Preston
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay so now I have rewritten my program and it works slightly better, but it still doesn't work that well. My objective is still to copy the message in one file to the other



No error pops up this time but the the file that I am copying to just puts a load of H's in the txt file. The message in the test.txt file is "Hello World" so as you can see the problem is that either the input isn't reading the whole text file or the output isn't able to output the text. Thanks for any suggestions on how to fix this
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You only read the first character now. In your previous post you called input.read() during each while loop iteration.
reply
    Bookmark Topic Watch Topic
  • New Topic