如何使用 config.property 文件将任何类型的文件(例如 txt 文件)写入资源文件夹,但不使用 java 中的绝对路径文件
How to write any type of file (for example txt file) to a resources folder with the config.property file, but without using absolute path file in java
如何将任何类型的文件(例如.txt
文件)写入或读取到资源文件夹config.property
文件,但没有使用绝对路径文件。
我试过如下解决这个问题:
ClassLoader classLoader = Setting.class.getClassLoader();
Setting setting = new Setting();
try (InputStream resourceAsStream = classLoader.getResourceAsStream("config.properties")) {
setting.load(resourceAsStream);
}
String readFileName = setting.getValue("pathSource.txt");
String writeFileName = setting.getValue("outPutPathSourceFile.txt");
String s = System.getProperty("line.separator");
File readFile = new File("./src/main/resources" + File.separator + "pathSource.txt");
File writeFile = new File("./src/main/resources" + File.separator + "outPutPathSourceFile.txt");
但是,我不想使用 ./src/main/resources
前缀。
如果您的文件位于 resources
下,您可以像这样访问它:
File file = new File(classLoader.getResource(fileName).getFile());
如果您使用 Spring,您可以使用 ResourceUtils.getFile():
File file = ResourceUtils.getFile("classpath:fileName")
更新:
如果我理解正确,您想读取位于 src/main/resources
下的文件而不使用相对路径 /src/main/resources
。
我创建了小演示:
public class ReadResourceFile {
public static void main(String[] args) throws IOException {
String fileName = "webAutomationConfig.xml";
ClassLoader classLoader = ReadResourceFile.class.getClassLoader();
File file = new File(classLoader.getResource(fileName).getFile());
System.out.println("File exists: " + file.exists());
String content = new String(Files.readAllBytes(file.toPath()));
System.out.println(content);
}
}
输出:
File exists: true
<?xml version="1.0" encoding="utf-8"?>
<config>
<baseUrl>https://www.gmail.com</baseUrl>
</config>
项目结构:
资源(class 路径上的文件,可能在 jar 中)可以读取,但不能写入。您可以使用它们作为模板在文件系统上创建初始副本。 – Joop Eggen
如何将任何类型的文件(例如.txt
文件)写入或读取到资源文件夹config.property
文件,但没有使用绝对路径文件。
我试过如下解决这个问题:
ClassLoader classLoader = Setting.class.getClassLoader();
Setting setting = new Setting();
try (InputStream resourceAsStream = classLoader.getResourceAsStream("config.properties")) {
setting.load(resourceAsStream);
}
String readFileName = setting.getValue("pathSource.txt");
String writeFileName = setting.getValue("outPutPathSourceFile.txt");
String s = System.getProperty("line.separator");
File readFile = new File("./src/main/resources" + File.separator + "pathSource.txt");
File writeFile = new File("./src/main/resources" + File.separator + "outPutPathSourceFile.txt");
但是,我不想使用 ./src/main/resources
前缀。
如果您的文件位于 resources
下,您可以像这样访问它:
File file = new File(classLoader.getResource(fileName).getFile());
如果您使用 Spring,您可以使用 ResourceUtils.getFile():
File file = ResourceUtils.getFile("classpath:fileName")
更新:
如果我理解正确,您想读取位于 src/main/resources
下的文件而不使用相对路径 /src/main/resources
。
我创建了小演示:
public class ReadResourceFile {
public static void main(String[] args) throws IOException {
String fileName = "webAutomationConfig.xml";
ClassLoader classLoader = ReadResourceFile.class.getClassLoader();
File file = new File(classLoader.getResource(fileName).getFile());
System.out.println("File exists: " + file.exists());
String content = new String(Files.readAllBytes(file.toPath()));
System.out.println(content);
}
}
输出:
File exists: true
<?xml version="1.0" encoding="utf-8"?>
<config>
<baseUrl>https://www.gmail.com</baseUrl>
</config>
项目结构:
资源(class 路径上的文件,可能在 jar 中)可以读取,但不能写入。您可以使用它们作为模板在文件系统上创建初始副本。 – Joop Eggen