File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

accessing current directory

 
Maarten Vergouwen
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Maarten Vergouwen
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is interesting, I never knew the NetBeans IDE ran each program using its own directory. I'm glad you got it worked out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic