[英]I cant display the output from properties file in java
我正在尝试从属性文件读取并在文本字段中显示输出,但它只给我空白页。 我知道代码不是最好的,但我不知道另一种方法。
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();
}
}
}
您的代码看起来不错。 也许您的文件是空的?
您可以尝试使用该代码来打印文件吗?
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();
}
}
}
}
}
空的? 您可以在下面的代码中添加数据:
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);
另外,我认为您可以跳过一步并使用:
taxMessage.setText(prop.getProperty("tax"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.