I'm trying to read from a properties file and displaying the output in a text field but it just gives me a blank page. I know the codes isn't the best but I don't know another way.
Double income = Double.parseDouble(totalField.getText());
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("intput.properties");
prop.load(input);
if (income <= 11000) {
taxMessage.setText(String.valueOf(Double.parseDouble(prop.getProperty("tax"))));
montaxMessage.setText("mon tax:" + String.valueOf(Double.parseDouble(prop.getProperty("tax")) / 12));
}
} catch(IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
Your code looks fine. Maybe your file is empty?
Can you try if that code is printing your file?
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class App {
public static void main(String[] args) {
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("input.properties");
prop.load(input);
// get the property value and print it out
System.out.println(prop.getProperty("tax"));
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Empty? You can add data the code below:
output = new FileOutputStream("config.properties");
// set the properties value
prop.setProperty("database", "localhost");
prop.setProperty("dbuser", "mkyong");
prop.setProperty("dbpassword", "password");
// save properties to project root folder
prop.store(output, null);
Also I think you can skip one step and use:
taxMessage.setText(prop.getProperty("tax"));
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.