Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem running ByteStream example code in eclipse  RSS feed

 
Rajiv Rai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I am getting FileNotFoundException when i run the following code in eclipse.



The error is as follows
Exception in thread "main" java.io.FileNotFoundException: xanadu.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 com.rajiv.io.CopyBytes.main(CopyBytes.java:14)


I have put the "xanadu.txt" file in the same package where this code exists. i.e under the com folder.

Do I need to put it somewhere else?

Thanks
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try placing your file(s) in directory returned by System.getProperty("user.dir");.
 
Rajiv Rai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Kemal it works.. but just for understanding
where do we need to put the files while working
in and IDE like eclipse.

 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When using an IDE you should place files you're trying to read in the root directory of your project. If you're using command line the file should be in the same folder (package) where the .class file you're reading from is.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To put it differently: the IDE uses a different folder structure, so the location of the file has to match that folder structure. You could use a file chooser to find your files, instead.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You could use a file chooser to find your files, instead.


@Rajiv Rai:

Or you could use an absolute path. Or you could look at the "Run Configurations..." selection on the "Run" menu in Eclipse, where I think you can view and change the current directory.

Whenever you specify relative path to a file, rather than an absolute path, it has to be realtive to something. When running a command in a terminal window, relative paths are taken to be relative to the current working directory--the directory you're "in" when you invoke the command. When launching Java from an IDE, however, it's not obvious what the "current" directory will be. It's basically whatever the IDE chooses it to be, though by default it's probably something sensible like the project's root directory. If it is, though, that's only by convention.
 
Rajiv Rai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have another doubt in this at the following line

out.write(c);

'c' is defined as an int so how and where does the conversion
from int to char happens when writing the output?

@Campbell
How does using a filechooser helps here?
i just had a brief glimpse of that and that is some GUI related API or concept
How does it relate to what am doing here ?
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A file chooser would help you to find the correct file. Once the file is found, there is no problem about non‑existent files.
You have got at least three different suggestions, which would each help you to sort out your problem. Thank you Kemal and Jeff.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajiv Rai wrote:I have another doubt in this at the following line

out.write(c);

'c' is defined as an int so how and where does the conversion
from int to char happens when writing the output?


There is no conversion from int to char. If you read the docs for that method, and then click the link where it says it's implementing the method defined in the parent class, you'll see that a conversion from int to byte is taking place.

As for how, it describes the end result, so we can take an educated guess that it would be by casting, and as for where, well, obviously it must be inside that method, or in a method it calls. The source code is in the src.zip file that came with the JDK download, so you can look for yourself.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!