File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes accessing current directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "accessing current directory" Watch "accessing current directory" New topic
Author

accessing current directory

Maarten Vergouwen
Ranch Hand

Joined: Jan 12, 2004
Posts: 60
what is the easiest way of looking for a file in your current directory (ie, the directory the Java program is executing from) ?
On a related subject (as this was my forst guess at how to achive the above), if you specify a file without any directory info as being say "myfile.txt", where does Java look for it?
Donny Nadolny
Ranch Hand

Joined: Mar 05, 2003
Posts: 32
To get the current directory:

Example output: C:\My Java Stuff\Sample Programs\CurrentDir Program
From that, you could create a new file reader using the string containing the user's directory and append a slash (for windows) and then the filename.

<Edit>: By the way, I just checked, Java looks for a file in the current directory, so rather than using the code above you could simply use a file reader and pass the name of the file relative to the current directory.
[ February 08, 2004: Message edited by: Donny Nadolny ]

- Donny Nadolny<br />The pen is mightier than the sword, and considerably easier to write with.
Maarten Vergouwen
Ranch Hand

Joined: Jan 12, 2004
Posts: 60
That's odd, I tried it as well and it didnt find the file.
location of the .class I am excuting C:\java\projects\myproject\
Image i = Toolkit.getDefaultToolkit().getImage("MyLogo.jpg"); <--- not found
Image i = Toolkit.getDefaultToolkit().getImage("C:\\java\\projects\\myproject\\MyLogo.jpg"); <---- found

I'll have to do some experimenting I guess :/
[ February 08, 2004: Message edited by: Maarten Vergouwen ]
Maarten Vergouwen
Ranch Hand

Joined: Jan 12, 2004
Posts: 60
Amusing.
I am using NetBeans IDE, and apparently when you run an application, no matter where its source/class files are located, it takes the netbeans\bin directory as its current dir... if I put the pic in there, it loads.

Edit: if I run the program outside of the IDE, it works. So it wasnt a problem to begin with, heh.
Thanks Donny!
[ February 08, 2004: Message edited by: Maarten Vergouwen ]
Donny Nadolny
Ranch Hand

Joined: Mar 05, 2003
Posts: 32
That is interesting, I never knew the NetBeans IDE ran each program using its own directory. I'm glad you got it worked out.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: accessing current directory
 
Similar Threads
FileNotFound
Why does java not allow multiple public classes in a java file ?
what does this error message mean?
Path for Temporary Directory