开发者

How can I save a file to the class path [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

开发者_开发知识库

Closed 2 years ago.

Improve this question

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.


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.

Related questions:

  • getResourceAsStream() versus FileInputStream


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");


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.

Read Properties from Classpath:

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();
    }
}

Write Properties to Classpath:

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();
}

Create the File Object on the Classpath:

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");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜