• 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

accessing current directory

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic