Saad Ahmad Saad Ahmad - 26 days ago 5x
Java Question

Get file from project folder java

I want to get File from project folder by using "File class" , How I can Do that ?

File file=new File("x1.txt");


Well, there are many different ways to get a file in Java, but that's the general gist.

Don't forget that you'll need to wrap that up inside a try {} catch (Exception e){} at the very least, because File is part of which means it must have try-catch block.

Not to step on Ericson's question, but if you are using actual packages, you'll have issues with locations of files, unless you explicitly use it's location. Relative pathing gets messed up with Packages.



In this example, using File f = new File("x.txt"); inside of will throw a file-not-found exception.

However, using File f = new File("src/x.txt"); will work.

Hope that helps!