I want to get File from project folder by using \"File class\" , How I can Do that ?
File file=new File(\"x1.txt\");
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 are trying to load a file which is not in the same directory as your Java class, you've got to use the runtime directory structure rather than the one which appears at design time.
To find out what the runtime directory structure is, check your {root project dir}/target/classes directory. This directory is accessible via the "." URL.
Based on user4284592's answer, the following worked for me:
ClassLoader cl = getClass().getClassLoader();
File file = new File(cl.getResource("./docs/doc.pdf").getFile());
with the following directory structure:
{root dir}/target/classes/docs/doc.pdf
Here's an explanation, so you don't just blindly copy and paste my code:
in javaFx 8
javafx.scene.image.Image img = new javafx.scene.image.Image("/myPic.jpg", true);
URL url = new URL(img.impl_getUrl());
Just did a quick google search and found that
System.getProperty("user.dir");
returns the current working directory as String. So to get a File out of this, just use
File projectDir = new File(System.getProperty("user.dir"));
Given a file application.yaml
in test/resources
ll src/test/resources/
total 6
drwxrwx--- 1 root vboxsf 4096 Oct 6 12:23 ./
drwxrwx--- 1 root vboxsf 0 Sep 29 17:05 ../
-rwxrwx--- 1 root vboxsf 142 Sep 22 23:59 application.properties*
-rwxrwx--- 1 root vboxsf 78 Oct 6 12:23 application.yaml*
-rwxrwx--- 1 root vboxsf 0 Sep 22 17:31 db.properties*
-rwxrwx--- 1 root vboxsf 618 Sep 22 23:54 log4j2.json*
From the test context, I can get the file with
String file = getClass().getClassLoader().getResource("application.yaml").getPath();
which will actually point to the file in test-classes
ll target/test-classes/
total 10
drwxrwx--- 1 root vboxsf 4096 Oct 6 18:49 ./
drwxrwx--- 1 root vboxsf 4096 Oct 6 18:32 ../
-rwxrwx--- 1 root vboxsf 142 Oct 6 17:35 application.properties*
-rwxrwx--- 1 root vboxsf 78 Oct 6 17:35 application.yaml*
drwxrwx--- 1 root vboxsf 0 Oct 6 18:50 com/
-rwxrwx--- 1 root vboxsf 0 Oct 6 17:35 db.properties*
-rwxrwx--- 1 root vboxsf 618 Oct 6 17:35 log4j2.json*
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!