【发布时间】:2014-11-09 12:04:55
【问题描述】:
当我将它作为 jar 运行时,会在桌面上创建这个 .properties 文件。为了保持清洁,如何设置路径以将此文件保存在其他位置,例如文件夹?甚至是罐子本身,但我无法让它发挥作用。我打算把它交给某人,并且不希望他们的桌面在 .properties 文件中杂乱无章..
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
public class DataFile {
public static void main(String[] args) {
Properties prop = new Properties();
OutputStream output = null;
try {
output = new FileOutputStream("config.properties");
prop.setProperty("prop1", "000");
prop.setProperty("prop2", "000");
prop.setProperty("prop3", "000");
prop.store(output, null);
} catch (IOException io) {
io.printStackTrace();
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
【问题讨论】:
-
它是干什么用的?也许Preferences API 会更合适?
-
属性文件?我只是用它来存储用户输入的一些信息,这样下次程序运行时,这些信息就不会被删除。
-
您有两个选择,更改启动程序的链接的“开始”引用或指定绝对路径引用