如何将文件保存到类路径

发布于 2024-10-12 09:51:10 字数 1539 浏览 5 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

无悔心 2024-10-19 09:51:11

使用 ClassLoader#getResource()getResourceAsStream() 从类路径获取它们作为 URLInputStream

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("com/example/file.ext");
// ...

或者,如果它与当前类在同一个包中,您也可以通过以下方式获取它:

InputStream input = getClass().getResourceAsStream("file.ext");
// ...

保存是一个故事。如果文件位于 JAR 文件中,这将不起作用。如果您可以确保文件已展开且可写,请将 URLgetResource() 转换为 File

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

然后,您可以构造一个 FileOutputStream与它一起。

相关问题:

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:

多像笑话 2024-10-19 09:51:11

如果您的类是从文件系统加载的,您可以尝试以下操作。

String basePathOfClass = getClass()
   .getProtectionDomain().getCodeSource().getLocation().getFile();

要获取该路径中的文件,您可以使用

File file = new File(basePathOfClass, "filename.ext");

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");
网名女生简单气质 2024-10-19 09:51:11

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

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.

清君侧 2024-10-19 09:51:11

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

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

绝情姑娘 2024-10-19 09:51:11

这是 Peter 响应的扩展:

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

如果您希望文件位于不同的类路径中(示例:progect/test-classes),只需替换this.getClass() 与类似 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);
}

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);
}
丿*梦醉红颜 2024-10-19 09:51:11

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

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

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

string classPath = System.getProperty("java.class.path");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文