Link to home
Start Free TrialLog in
Avatar of JAVAnewbie
JAVAnewbie

asked on

How to get a file's path when u only know its name?

not sure if the title is appropriate...

this is what i wanted to get:
i have a text file: output.txt
and it will be put along with all my application class file inside a folder - "whatever".

and since this "whatever" folder will be move to different directory very frequently, how am i going to keep track on the output.txt file path?

i tried using getClass().getResource("output.txt") but failed.



String filepath = ?? //how to locate the file?

File f = new File(filepath);

please help

SOLUTION
Avatar of riaancornelius
riaancornelius

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of riaancornelius
riaancornelius

File f = new File(System.getProperty("user.dir")+File.separator+"output.txt");
String filepath = System.getProperty("user.dir")+File.separator+"output.txt";
File f = new File(filepath);
Avatar of JAVAnewbie

ASKER

no, i don't want to go to Properties first....
i just wanted to know if there's any way i could get the path for any files that store in the same folder as my class file ;)
that's the way to do it.

System.getProperty("user.dir") returns the path your class is running from.

other than that, I have no idea.
String path = getClass().getResource("/app/files/output.txt").toString();

java.io.FileNotFoundException: file:\C:\Documents%20and%20Settings\app\files\output.txt
Properties is good, but what if someone move the "whatever" folder to another directory before setting the properties??
that property is set automatically to whatever folder your app is running from.
I've been using that for a long time, and it's always worked
opps, sorry, i think i misunderstand ur comments ;p
let me try it out first, will let u know soon.
deeply regret for not really look into ur comments before i post mine...
but now i got another related problem..
i'm actually using netbean, and my project folder - "Whatever" is store in
- C:\Documents and Settings\guest\Whatever

and my output.txt will be inside:
- C:\Documents and Settings\guest\Whatever\build\classes\package\files\output.txt

and when i run the code u suggested and print it:
            String a = System.getProperty("user.dir");
            System.out.println(a);

the output is:
- C:\Documents and Settings\guest\Whatever

so if i add my file name ("output.txt") right after the user.dir... it wont work!

just add the whole thing:
String path = System.getProperty("user.dir")+"\\build\\classes\\package\\files\\output.txt";
hmm...then one day, one fella move the package folder some where else and use command to run it.. it won't work again. becoz the build\\classes\\ wont be applicable anymore..
just keep output.txt in the same folder as your app's entry class, that way you will always know where it is.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial