I Exported my program into a jar file but when I ran it, it seems as if the .properties file isn't found by the program. I made sure it was in the jar file & it worked fine prior to exporting it. I read something about using getClass().getResourceAsStream()
instead of FileInputStream
and FileOutputStream
but can't seem to understand how that would help. Any ideas? These are the two methods that use the file.
private void UpdateData() throws IOException{
FileInputStream in = new FileInputStream("config.properties");
Properties props = new Properties();
props.load(in);
in.close();
FileOutputStream out = new FileOutputStream("config.properties");
props.setProperty("prop1", prop1TextArea.getText().toString());
props.setProperty("prop2", prop2TextArea.getText().toString());
props.setProperty("prop3", prop3TextArea.getText().toString());
props.store(out, null);
out.close();
}
private void setText() throws IOException {
FileInputStream in = new FileInputStream("config.properties");
Properties props = new Properties();
props.load(in);
in.close();
FileOutputStream out = new FileOutputStream("config.properties");
prop1TextArea.setText(props.getProperty("prop1"));
prop2TextArea.setText(props.getProperty("prop2"));
prop3TextArea.setText(props.getProperty("prop3"));
out.close();
}
To access resources in your .jar
file you have to access them as resources not as files.
The .properties
are not in the filesystem anymore and therefore you cannot access them via FileInputStream
.
If you want to save them afterwards you have to create new .properties
on the first start of your program. So you have to check first if the file exists (eg via File.exists()
) and either work without the properties from the file or create a new file and use this then.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.