Java 从jar写入属性

Java 从jar写入属性,java,eclipse,Java,Eclipse,您好,我使用Eclipse用Java编写了一个游戏,我通过一个读/写配置txt文件的类来管理首选项。 当我从eclipse运行它时,一切正常,但当我从jar运行它时,我创建的读和写都不工作。 我使用getResourceAsStream解决了阅读问题,但我不知道如何解决写作问题。 这是我用来读取文件的代码: try { InputStream is = getClass().getResourceAsStream("/res/configuration.txt"); Prop

您好,我使用Eclipse用Java编写了一个游戏,我通过一个读/写配置txt文件的类来管理首选项。 当我从eclipse运行它时,一切正常,但当我从jar运行它时,我创建的读和写都不工作。 我使用getResourceAsStream解决了阅读问题,但我不知道如何解决写作问题。 这是我用来读取文件的代码:

try {
     InputStream is = getClass().getResourceAsStream("/res/configuration.txt");
     Properties properties = new Properties();
     InputStreamReader isr = new InputStreamReader(is);
     BufferedReader br = new BufferedReader(isr);
     properties.load(br);
     br.close(); 
} 
try {
     properties.setProperty("playerColor", value);
     FileOutputStream fos = new FileOutputStream("/res/configuration.txt");
     OutputStreamWriter osw = new OutputStreamWriter(fos);
     properties.store(osw, null);
     osw.close();
}
和properties.getProperty来获取值。 可以从Eclipse和jar中工作

这是我试图编写该文件的代码:

try {
     InputStream is = getClass().getResourceAsStream("/res/configuration.txt");
     Properties properties = new Properties();
     InputStreamReader isr = new InputStreamReader(is);
     BufferedReader br = new BufferedReader(isr);
     properties.load(br);
     br.close(); 
} 
try {
     properties.setProperty("playerColor", value);
     FileOutputStream fos = new FileOutputStream("/res/configuration.txt");
     OutputStreamWriter osw = new OutputStreamWriter(fos);
     properties.store(osw, null);
     osw.close();
}
在任何情况下都不起作用,它说它找不到文件

我怎样才能解决这个问题?此外,我必须在代码中使用类属性

谢谢

更多信息

我使用两个csv文件保存记录和游戏,并使用一个txt文件进行配置,它们以这种方式位于包“res”中:

Project Folder
    --- classpath
    --- bin
    --- src
        --- project
        --- res
            --- records.csv
            --- save.csv
            --- configuration.txt

首先,jar是一个归档文件,它不应该被更改。 因此,正确的解决方案是写入外部源。
您可以创建一个文件夹,并将游戏中的所有数据放入该文件夹(统计信息、保存文件等)

谢谢。因此,当我运行jar时,我不能使用我的“res”包中的文件,但我应该在pc的文件夹中创建它们的副本,并读/写它们?重新组织jar,现在一切正常。再次感谢。