my dog learned polymorphism
The moose likes Beginning Java and the fly likes Relative Addressing for .txt Files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Relative Addressing for .txt Files" Watch "Relative Addressing for .txt Files" New topic

Relative Addressing for .txt Files

David Dickinson
Ranch Hand

Joined: Nov 11, 2004
Posts: 66

I'd like to remove the use of absolute addressing in my program and use a relative path to my .txt file. At the moment I have a statement like so:

But I wan't this to change to reflect the file existing locally within my Eclipse Workspace files.

I've tried altering the path to simply "Garage.txt" which didn't work and i've tried "GarageAssignment\Garage.txt" but this didn't work either.

The file appears within Eclipse's Package Explorer next to my classes so its' recognising its existence.

Philip Shanks
Ranch Hand

Joined: Oct 15, 2002
Posts: 189
If you use a relative path name, then the only question is relative to what? The answer is, relative to where the process was launched. Given the following test code:

If you invoke the JVM from the directory that contains this package as

then you will see the file "test.txt" created in the dirctory just above, assuming that you aren't calling this from the root of the file system.

Philip Shanks, SCJP - Castro Valley, CA
My boss never outsources or has lay-offs, and He's always hiring. I work for Jesus! Prepare your resume!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
As Philip says, you need to find what Eclipse considered the "current working directory" when it launches your program. I'm not familiar with Eclipse, but in NetBeans, you can set the current working directory under Tools | Options. You should look in the Eclipse help files or browse its options in order to find what this setting is and how you can modify it.



Java API Documentation
The Java Tutorial
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Relative Addressing for .txt Files
It's not a secret anymore!