referring to file in a relative way

A

angelochen960

Hi,

in Unit test, my test class is in:

src/test/java/org/...

and I offer need to refer to a file in
src/test/data

following is how i specify, but that's absolute path, any way to refer
it relatively so that I can move the test around? Thanks

String filename = "/Users/angelochen/testt5/myProject/src/test/data/
info.txt";
 
R

Roedy Green

following is how i specify, but that's absolute path, any way to refer
it relatively so that I can move the test around? Thanks

one way to do it is carry your filenames around in two pieces, and
glue them together with the File constructor.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Don’t worry about the world coming to an end today.
It is already tomorrow in Australia."
~ Charles Schulz
 
A

Angelo Chen

I use:

userdir = System.getProperties("user.dir");

that works, was thinking if there is a simpler way.
 
H

Hal Rosser

Hi,

in Unit test, my test class is in:

src/test/java/org/...

and I offer need to refer to a file in
src/test/data

following is how i specify, but that's absolute path, any way to refer
it relatively so that I can move the test around? Thanks

String filename = "/Users/angelochen/testt5/myProject/src/test/data/
info.txt";

If you're in the org directory then
File theFile = new File("../../data/info.txt");
Then you can use methods of the File class to get the absolute path, etc if
you need it.
Each dot-dot-slash takes you to the parent directory of wherever you happen
to be,
HTH.
 
A

Arved Sandstrom

Hi,

in Unit test, my test class is in:

src/test/java/org/...

and I offer need to refer to a file in
src/test/data

following is how i specify, but that's absolute path, any way to refer
it relatively so that I can move the test around? Thanks

String filename = "/Users/angelochen/testt5/myProject/src/test/data/
info.txt";
Take the part of the path that's likely to change and make it a property
in a property file somewhere. You can take this a bit further if you're
changing the location of tests within a project, and the project
location is itself changing...just use two properties.

AHS
 
L

Lew

Hal said:
If you're in the org directory then
File theFile = new File("../../data/info.txt");
Then you can use methods of the File class to get the absolute path, etc if
you need it.
Each dot-dot-slash takes you to the parent directory of wherever you happen
to be,

Which if it's close to the root will cause a problem.

Use of parent-directory relative references is not very safe. One
should ensure that all program resources are in a subdirectory of the
working directory so that "dot-dot-slash" is never used.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top