This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading a file in current folder  RSS feed

 
Carlos Delgado
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!
I'm having troubles reading a file which is in the current folder of the program.
I make the file and the stream, but if I specify the path as "file.ext" my program doesn't reads it: I have to specify the whole path as "C:\\MyProgram\\file.ext". I tried so "\\file.ext" but I got the same error.
I'm using netBeans. Could it be the problem? Does anyone know what's happening?

Thank you very much in advance!
 
jefff willis
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you are getting a file not found exception then you are not in the directory where the file is.
 
Jayesh Lalwani
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had to learn this the hard way: NEVER open files from current folder. Alsways specify the full path. Not only does your application fail if the user starts your app from some other folder, but Windows has a nasty habit of changing the current folder for you
 
Carlos Delgado
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a function which returns the current path of the program?? So I know if it's working in the correct folder or not... I know it seems a very newbie question: I'm a very newbie Java programmer...
Thank you again!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this on Google:

I recall doing something just like that when I first got into Java. This one seems much more "correct"


Anyone have a good resource for all the standard property names? Things like this seem overly hard to find.

D'oh! Google and you shall receive: http://java.sun.com/docs/books/tutorial/essential/system/properties.html
[ November 11, 2004: Message edited by: Stan James ]
 
Carlos Delgado
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stan!
Your code was useful for knowing the current folder of the program. The problem is NetBeans. I got that the current dir is "C:\Program files\NetBeans\bin". I compiled the program in console mode and it works great.
So the problem is solved.

Thank you all!!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Stan]: Anyone have a good resource for all the standard property names?

Aside from the tutorial, there's a more current list in the API for System under getProperties(). They've added a few since the tutorial was written.
[ November 20, 2004: Message edited by: Jim Yingst ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!