How can I save / load a file that is located where my classes are? I don't the physical path to that location before and I want dynamically to find that file.
I want to load an XML file and write and read to it and I am not sure how to address it.
How can I save / load a file that is located where my classes are? I don't the physical path to that location before and I want dynamically to find that file.
I want to load an XML file and write and read to it and I am not sure how to address it.
In the general case you cannot. Resources loaded from a classloader can be anything: files in directories, files embedded in jar files or even downloaded over the network.
Use ClassLoader#getResource()
or getResourceAsStream()
to obtain them as URL
or InputStream
from the classpath.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("com/example/file.ext");
// ...
Or if it is in the same package as the current class, you can also obtain it as follows:
InputStream input = getClass().getResourceAsStream("file.ext");
// ...
Saving is a story apart. This won't work if the file is located in a JAR file. If you can ensure that the file is expanded and is writable, then convert the URL
from getResource()
to File
.
URL url = classLoader.getResource("com/example/file.ext");
File file = new File(url.toURI().getPath());
// ...
You can then construct a FileOutputStream
with it.
url.toURI()
- There will be NullPointerException
if file doesn't exist. –
Newell url
is not null
in case you're unsure. It's not different for any other Java variable which you're attempting to access using period operator .
. Every self-respected Java developer knows this. –
Halfblooded getResource
returning null. –
Sulphonamide URISyntaxException
. –
Ichthyology You can try the following provided your class is loaded from a filesystem.
String basePathOfClass = getClass()
.getProtectionDomain().getCodeSource().getLocation().getFile();
To get a file in that path you can use
File file = new File(basePathOfClass, "filename.ext");
File file = new File(basePathOfClass + "Filename.txt");
–
Ichthyology new File(".").getAbsolutePath() + "relative/path/to/your/files";
In the general case you cannot. Resources loaded from a classloader can be anything: files in directories, files embedded in jar files or even downloaded over the network.
This is an expansion on Peter's response:
If you want the file in the same classpath as the current class (Example: project/classes):
URI uri = this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI();
File file = new File(new File(uri), PROPERTIES_FILE);
FileOutputStream out = new FileOutputStream(createPropertiesFile(PROPERTIES_FILE));
prop.store(out, null);
If you want the file in a different classpath (Example: progect/test-classes), just replace this.getClass()
with something like TestClass.class
.
Properties prop = new Properties();
System.out.println("Resource: " + getClass().getClassLoader().getResource(PROPERTIES_FILE));
InputStream in = getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE);
if (in != null) {
try {
prop.load(in);
} finally {
in.close();
}
}
Properties prop = new Properties();
prop.setProperty("Prop1", "a");
prop.setProperty("Prop2", "3");
prop.setProperty("Prop3", String.valueOf(false));
FileOutputStream out = null;
try {
System.out.println("Resource: " + createPropertiesFile(PROPERTIES_FILE));
out = new FileOutputStream(createPropertiesFile(PROPERTIES_FILE));
prop.store(out, null);
} finally {
if (out != null) out.close();
}
private File createPropertiesFile(String relativeFilePath) throws URISyntaxException {
return new File(new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI()), relativeFilePath);
}
According to system properties documentation, you can access this as the "java.class.path" property:
string classPath = System.getProperty("java.class.path");
© 2022 - 2024 — McMap. All rights reserved.