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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

use the System.getProperty("user.dir") method to get path of folder your app is running from.
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);
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

JAVAnewbieAuthor Commented:
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.
JAVAnewbieAuthor Commented:
String path = getClass().getResource("/app/files/output.txt").toString(); file:\C:\Documents%20and%20Settings\app\files\output.txt
JAVAnewbieAuthor Commented:
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
JAVAnewbieAuthor Commented:
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...
JAVAnewbieAuthor Commented:
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");

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";
JAVAnewbieAuthor Commented:
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.
How can you locate a file that some people move around at will?

There's 2 solutions:
1) demand that the file is beneath the class file (AND THAT IS NOT via "user.dir" !!!!!!!)
2) do a recursive search over the system (crazy indeed).

Your very first approach via the class file is the best.

I cut this from one of my applications, why should it not work for you?

    * Safely get the path to a resource file via Class info.
    * <p>If the file is missing, assuming that the resource was an image file,<BR>
    * the packaged <b>MISSING.gif</b> icon is substituted,
    * using the class info <b>packageClass</b> retrieved elsewhere.</p>
    * @param  classinfo   a Class loaded from the root of relative pathname.
    * @param  filename    the relative pathname to the resource file.
    * @return             an URL to the resulting resource.
   private static URL findResource(Class classInfo, String filename)
   URL resourceURL;

      if(classInfo == null)
         fatal("BAD CLASS INFO");

      if((resourceURL = classInfo.getResource(filename)) == null)
         error("RESOURCE FILE '" + filename + "' MISSING.");
            = packageClass.getResource("res/MISSING.gif")) == null)

// And deploying this in my program elsewhere (assume JFrame mainFrame;   ):

    * Set the default icon on the program main JFrame.
    * <p>Can be used to reset the icon after calls with an argument.<BR>
    * The default icon should be <b>res/<program>.gif</b>.</p>
   public static void setMainFrameIcon( )
         Toolkit.getDefaultToolkit().getImage(findResource(this.getClass( ), "res/Application.gif"))

consider this ( i tested it in Windows platform ONLY):

        String path = getClass().getResource(filename).toString(); //*
        path = path.substring(6);
        path = path.replace("%20", " ");
       File f = new File(path);

* The filename can be the file name it self, or the relative file name Eg: if its with ur classes file, "output.txt", if its a directory under ur class files, "output/output.txt".

first, u get the url, then convert it to String. however, this String is preceed with
so, u remove the "file:/" using substring(6)
and now, ur path will be starting with c:\....

however, space in between will be replace by "%20"... get rid of it by replacing them with empty space and u will have the original path now.

after that, just create ur File.

hope it helps!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.