I want to get File from project folder by using "File class", How can I do that?
File file=new File("x1.txt");
I want to get File from project folder by using "File class", How can I 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 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!
File
itself does not throw any IOException
s. Some of it's methods do, but simply creating a File
object does not require any try-catch
...It's also a bad idea to reference the src
directory as it will not be available when the application is build and deployed. –
Sawyor 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
{project directory}/somewhere/X.txt
and I try to access that from {project directory}/src/java/GetX.java
. How would that look like? –
Esperanzaespial ./somewhere/X.txt
), but as I just stated, this breaks the moment the working directory changes. You could use an absolute path, but the problem becomes, where do you store the file. –
Sawyor 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).
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:
String path = System.getProperty("user.dir")+"/config.xml";
File f=new File(path);
resources
folder, but just in the root folder. –
Ulent These lines worked in my case,
ClassLoader classLoader = getClass().getClassLoader();
File fi = new File(classLoader.getResource("test.txt").getFile());
provided test.txt file inside src
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*
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"));
It is not trivial task (for any location for file request), unfortunately. Java is amazing but this simplest question places to confusion... Try start by this approach:
Path generatorPath;
generatorPath = Path.of(System.getProperty("user.dir") + GetPath(ConfigEnum.Debug));
Also, take into account Debug and Production variants
public String GetPath(ConfigEnum config){
if (config == ConfigEnum.Artifacts) {
return "file:folder/file.txt";
} else {
return "/folder/file.txt";
}
}
public enum ConfigEnum {
Debug,
Artifacts
}
© 2022 - 2024 — McMap. All rights reserved.