Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我需要覆盖属性文件中的值_Java - Fatal编程技术网

Java 我需要覆盖属性文件中的值

Java 我需要覆盖属性文件中的值,java,Java,我的属性文件包含3个属性,我需要覆盖thirdOne的值。如何从java代码中的类路径加载属性文件并覆盖它 我的属性文件位置PackageName->resource->folderName->.propertyFile 属性文件:我需要覆盖“epochromtime”的值: FILE_PATH=C:\\Users\\pda\\Desktop\\JsonOutput\\DataExtract epochFilename=C:\\Users\\pda\\Desktop\\JsonOutput\\e

我的属性文件包含3个属性,我需要覆盖thirdOne的值。如何从java代码中的类路径加载属性文件并覆盖它

我的属性文件位置PackageName->resource->folderName->.propertyFile

属性文件我需要覆盖“epochromtime”的值

FILE_PATH=C:\\Users\\pda\\Desktop\\JsonOutput\\DataExtract
epochFilename=C:\\Users\\pda\\Desktop\\JsonOutput\\epochTime.txt
epochFromTime=1545329531862
Java代码:

try {
            Properties config = new Properties();
                config.load(ClassLoader.getSystemResourceAsStream(PROPERTIES_PATH));
                String epochFromTimeChanged= Long.toString(epoch_to2);
                config.setProperty("epochFromTime",epochFromTimeChanged);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 

这很容易。首先,请将属性文件读取到
属性
。然后更新它,然后保存它。不要忘记关闭资源

public static void updateProperties(File file, Consumer<Properties> consumer) throws IOException {
    Properties properties = new Properties();

    try (Reader reader = new BufferedReader(new FileReader(file))) {
        properties.load(reader);
    }

    consumer.accept(properties);

    try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
        properties.store(writer, "comment");
    }

}

应用程序属性

更新前

更新后

使用
Properties.store()
将更改后的值写回文件:

String PROPERTIES_PATH = "...";
try {
    File f = new File(PROPERTIES_PATH);
    FileInputStream in = new FileInputStream(f);
    Properties config = new Properties();
    config.load(in);

    String epochFromTimeChanged= Long.toString(epoch_to2);
    config.setProperty("epochFromTime",epochFromTimeChanged);

    // get or create the file
    File f = new File(PROPERTIES_PATH);
    OutputStream out = new FileOutputStream(f);
    config.store(out, "My properties file comment");

} catch (FileNotFoundException e1) {
    log.info("{} does not exist", PROPERTIES_PATH);
} catch (IOException e) {
    log.error("Cannot access {}", PROPERTIES_PATH, e);
}

你试过什么,什么不起作用?不,它不起作用。你能告诉我如何使用FileOutputStream来实现这一点吗?关于OutputStream的使用有很多例子。我相信你很容易就能找到它们。对于属性,如果您使用外部库,它将被指定用于管理它们。仅仅说“它不工作”对我们没有任何帮助。请查看这篇文章,并发布一个演示您正面临的确切问题的帖子…您只是从文件加载属性,然后更改内存中的值。您的代码不会将任何内容保存到文件中,也没有理由认为这只是奇迹般地发生了。你必须尝试一些东西,而不是说“它不起作用”。
two=two_two
one=one_one
#comment
#Fri Jan 04 18:59:12 MSK 2019
two=two_two_two
one=one_one
three=three_three
String PROPERTIES_PATH = "...";
try {
    File f = new File(PROPERTIES_PATH);
    FileInputStream in = new FileInputStream(f);
    Properties config = new Properties();
    config.load(in);

    String epochFromTimeChanged= Long.toString(epoch_to2);
    config.setProperty("epochFromTime",epochFromTimeChanged);

    // get or create the file
    File f = new File(PROPERTIES_PATH);
    OutputStream out = new FileOutputStream(f);
    config.store(out, "My properties file comment");

} catch (FileNotFoundException e1) {
    log.info("{} does not exist", PROPERTIES_PATH);
} catch (IOException e) {
    log.error("Cannot access {}", PROPERTIES_PATH, e);
}