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("ou tput.txt") but failed.
String filepath = ?? //how to locate the file?
File f = new File(filepath);
please help
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("ou
String filepath = ?? //how to locate the file?
File f = new File(filepath);
please help
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
File f = new File(System.getProperty("u ser.dir")+ File.separ ator+"outp ut.txt");
String filepath = System.getProperty("user.d ir")+File. separator+ "output.tx t";
File f = new File(filepath);
File f = new File(filepath);
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 ;)
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.d ir") returns the path your class is running from.
other than that, I have no idea.
System.getProperty("user.d
other than that, I have no idea.
ASKER
String path = getClass().getResource("/a pp/files/o utput.txt" ).toString ();
java.io.FileNotFoundExcept ion: file:\C:\Documents%20and%2 0Settings\ app\files\ output.txt
java.io.FileNotFoundExcept
ASKER
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
ASKER
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...
let me try it out first, will let u know soon.
deeply regret for not really look into ur comments before i post mine...
ASKER
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\bu ild\classe s\package\ files\outp ut.txt
and when i run the code u suggested and print it:
String a = System.getProperty("user.d ir");
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!
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\bu
and when i run the code u suggested and print it:
String a = System.getProperty("user.d
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.d ir")+"\\bu ild\\class es\\packag e\\files\\ output.txt ";
String path = System.getProperty("user.d
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.