如何将文件保存到类路径

时间:2023-01-29 09:55:16

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.

如何保存/加载位于我的课程所在的文件?我之前没有到该位置的物理路径,我想动态地找到该文件。

Thanks

Edit:

I want to load an XML file and write and read to it and i am not sure how to address it.

我想加载一个XML文件并写入和读取它,我不知道如何解决它。

6 个解决方案

#1


6  

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.

在一般情况下,你不能。从类加载器加载的资源可以是任何内容:目录中的文件,嵌入在jar文件中的文件,甚至通过网络下载的文件。

#2


36  

Use ClassLoader#getResource() or getResourceAsStream() to obtain them as URL or InputStream from the classpath.

使用ClassLoader#getResource()或getResourceAsStream()从类路径中获取它们作为URL或InputStream。

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.

拯救是一个独特的故事。如果文件位于JAR文件中,则无效。如果可以确保文件已展开且可写,则将URL从getResource()转换为File。

URL url = classLoader.getResource("com/example/file.ext");File file = new File(url.toURI().getPath());// ...

You can then construct a FileOutputStream with it.

然后,您可以使用它构造FileOutputStream。

Related questions:

#3


11  

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

#4


7  

new File(".").getAbsolutePath() + "relative/path/to/your/files";

new File(“。”)。getAbsolutePath()+“relative / path / to / your / files”;

#5


4  

This is an expansion on Peter's response:

这是对Peter的回应的扩展:

If you want the file in the same classpath as the current class (Example: project/classes):

如果希望文件与当前类位于同一类路径中(例如: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.

如果您希望文件位于不同的类路径中(例如:progect / test-classes),只需用TestClass.class替换this.getClass()即可。

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

#6


1  

According to system properties documentation, you can access this as the "java.class.path" property:

根据系统属性文档,您可以将其作为“java.class.path”属性进行访问:

string classPath = System.getProperty("java.class.path");

#1


6  

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.

在一般情况下,你不能。从类加载器加载的资源可以是任何内容:目录中的文件,嵌入在jar文件中的文件,甚至通过网络下载的文件。

#2


36  

Use ClassLoader#getResource() or getResourceAsStream() to obtain them as URL or InputStream from the classpath.

使用ClassLoader#getResource()或getResourceAsStream()从类路径中获取它们作为URL或InputStream。

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.

拯救是一个独特的故事。如果文件位于JAR文件中,则无效。如果可以确保文件已展开且可写,则将URL从getResource()转换为File。

URL url = classLoader.getResource("com/example/file.ext");File file = new File(url.toURI().getPath());// ...

You can then construct a FileOutputStream with it.

然后,您可以使用它构造FileOutputStream。

Related questions:

#3


11  

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

#4


7  

new File(".").getAbsolutePath() + "relative/path/to/your/files";

new File(“。”)。getAbsolutePath()+“relative / path / to / your / files”;

#5


4  

This is an expansion on Peter's response:

这是对Peter的回应的扩展:

If you want the file in the same classpath as the current class (Example: project/classes):

如果希望文件与当前类位于同一类路径中(例如: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.

如果您希望文件位于不同的类路径中(例如:progect / test-classes),只需用TestClass.class替换this.getClass()即可。

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

#6


1  

According to system properties documentation, you can access this as the "java.class.path" property:

根据系统属性文档,您可以将其作为“java.class.path”属性进行访问:

string classPath = System.getProperty("java.class.path");