如何将文件附加到可以在此 jar 中编辑的 jar?
How to attach file to jar that can be edited inside this jar?
我正在制作一个与 MySQL 数据库一起使用的程序,目前我将 URL、登录名、密码 e.t.c 存储为 public static String
。现在我需要在另一台计算机上工作,所以数据库地址会有所不同,所以我需要一种在程序中编辑它并保存的方法。我只想使用外部 txt 文件,但我不知道如何指向它的位置。
我决定使用 属性 文件制作它,我把它放在 src/res
文件夹中。当我在 Intellij Idea 中尝试它时它工作正常,但是当我构建 jar(工件)时我得到 java.io.FileNotFoundException
我尝试了两种方法:
这个是刚刚复制的
私人字符串getFile(字符串文件名){</p>
<pre><code> StringBuilder result = new StringBuilder("");
//Get file from resources folder
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource(fileName).getFile());
System.out.println(file.length());
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
result.append(line).append("\n");
}
scanner.close();
} catch (IOException e) {
e.printStackTrace();
}
return result.toString();
}
System.out.println(obj.getFile("res/cfg.txt"));</code>
第二个使用 Properties
class:
try(FileReader reader = new FileReader("src/res/cfg.txt")) {
Properties properties = new Properties();
properties.load(reader);
System.out.println(properties.get("password"));
}catch (Exception e) {
e.printStackTrace();
System.out.println(e);
}
两种方式我都得到 java.io.FileNotFoundException
。这样附加配置文件的正确方法是什么?
由于文件在 .JAR 中,无法通过 new File()
访问它,但您仍然可以通过 ClassLoader
:
读取它
Properties properties = new Properties();
try (InputStream stream = getClass().getResourceAsStream("/res/cfg.txt")) {
properties.load(stream);
}
请注意,JAR 是只读的。所以这个方法行不通。
如果你想拥有可编辑的配置,你应该将你的 cfg.txt
放在 JAR 之外并从文件系统中读取它。例如像这样:
Properties properties = new Properties();
File appPath = new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParentFile();
try (InputStream stream = new FileInputStream(new File(appPath, "cfg.txt"))) {
properties.load(stream);
}
您可以在多个地方放置您的配置选项,并且稳健的部署策略将利用以下部分(或全部)技术:
正如我在评论中提到的,将配置文件存储在相对于用户主文件夹的众所周知的位置。这适用于 Windows (C:\Users\efrisch
)、Linux (/home/efrisch
) 和 Mac (/Users/efrisch
)
File f = new File(System.getProperty("user.home"), "my-settings.txt");
读取环境变量来控制它
File f = new File(System.getenv("DEPLOY_DIR"), "my-settings.txt");
使用 Apache ZooKeeper 等去中心化服务来存储您的数据库设置
使用Standalone JNDI
(或部署目标内置的 JNDI)
使用一个Connection Pool
我正在制作一个与 MySQL 数据库一起使用的程序,目前我将 URL、登录名、密码 e.t.c 存储为 public static String
。现在我需要在另一台计算机上工作,所以数据库地址会有所不同,所以我需要一种在程序中编辑它并保存的方法。我只想使用外部 txt 文件,但我不知道如何指向它的位置。
我决定使用 属性 文件制作它,我把它放在 src/res
文件夹中。当我在 Intellij Idea 中尝试它时它工作正常,但是当我构建 jar(工件)时我得到 java.io.FileNotFoundException
我尝试了两种方法:
这个是刚刚复制的
私人字符串getFile(字符串文件名){</p>
<pre><code> StringBuilder result = new StringBuilder("");
//Get file from resources folder
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource(fileName).getFile());
System.out.println(file.length());
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
result.append(line).append("\n");
}
scanner.close();
} catch (IOException e) {
e.printStackTrace();
}
return result.toString();
}
System.out.println(obj.getFile("res/cfg.txt"));</code>
第二个使用 Properties
class:
try(FileReader reader = new FileReader("src/res/cfg.txt")) {
Properties properties = new Properties();
properties.load(reader);
System.out.println(properties.get("password"));
}catch (Exception e) {
e.printStackTrace();
System.out.println(e);
}
两种方式我都得到 java.io.FileNotFoundException
。这样附加配置文件的正确方法是什么?
由于文件在 .JAR 中,无法通过 new File()
访问它,但您仍然可以通过 ClassLoader
:
Properties properties = new Properties();
try (InputStream stream = getClass().getResourceAsStream("/res/cfg.txt")) {
properties.load(stream);
}
请注意,JAR 是只读的。所以这个方法行不通。
如果你想拥有可编辑的配置,你应该将你的 cfg.txt
放在 JAR 之外并从文件系统中读取它。例如像这样:
Properties properties = new Properties();
File appPath = new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParentFile();
try (InputStream stream = new FileInputStream(new File(appPath, "cfg.txt"))) {
properties.load(stream);
}
您可以在多个地方放置您的配置选项,并且稳健的部署策略将利用以下部分(或全部)技术:
正如我在评论中提到的,将配置文件存储在相对于用户主文件夹的众所周知的位置。这适用于 Windows (
C:\Users\efrisch
)、Linux (/home/efrisch
) 和 Mac (/Users/efrisch
)File f = new File(System.getProperty("user.home"), "my-settings.txt");
读取环境变量来控制它
File f = new File(System.getenv("DEPLOY_DIR"), "my-settings.txt");
使用 Apache ZooKeeper 等去中心化服务来存储您的数据库设置
使用Standalone JNDI (或部署目标内置的 JNDI)
使用一个Connection Pool