Get file from project folder java
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 java.io
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.
ie,
src/
main.java
x.txt
In this example, using File f = new File("x.txt");
inside of main.java
will throw a file-not-found exception.
However, using File f = new File("src/x.txt");
will work.
Hope that helps!
This sounds like the file is embedded within your application.
You should be using getClass().getResource("/path/to/your/resource.txt")
, which returns an URL
or getClass().getResourceAsStream("/path/to/your/resource.txt");
If it's not an embedded resource, then you need to know the relative path from your application's execution context to where your file exists
If you don't specify any path and put just the file (Just like you did), the default directory is always the one of your project (It's not inside the "src" folder. It's just inside the folder of your project).