我试图从属性fie中读取但是它在这里不起作用是我的代码
File configFile = new File("intput.properties");
Properties prop = new Properties();
try
{
FileReader read = new FileReader(configFile);
if (income <= 11000)
{
tax = Double.parseDouble(prop.getProperty("tax", "0"));
taxMessage.setText("Annual tax:" + tax);
}
read.close();
}
catch (FileNotFoundException ex)
{
}
catch (IOException ex)
{
}
我没有收到任何错误,但它没有显示输出。
答案 0 :(得分:0)
您的属性尚未加载。见Properties#load(Reader)
prop.load(read);
File configFile = new File("intput.properties");
Properties prop = new Properties();
try {
FileReader read = new FileReader(configFile);
prop.load(read);
//the if block is not been executed when your income always > 11000 .
if (income <= 11000) {
tax = Double.parseDouble(prop.getProperty("tax", "0"));
taxMessage.setText("Annual tax:" + tax);
}
read.close();
} catch (Exception ex) { throw new RuntimeException(ex); }
答案 1 :(得分:0)
以下代码可以提供帮助。它是属性文件的位置或导致问题的加载方法。
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("C:\\temp\\intput.properties");
// load a properties file
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();
}
}
}