从java的属性文件中读取
我试图从一个属性五读,但它不工作这里是我的代码从java的属性文件中读取,java,Java,我试图从一个属性五读,但它不工作这里是我的代码 File configFile = new File("intput.properties"); Properties prop = new Properties(); try { FileReader read = new FileReader(configFile); if (income <= 11000) { tax = Double.parseDouble(prop.getProperty(
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)
{
}
File configFile=新文件(“intput.properties”);
Properties prop=新属性();
尝试
{
FileReader read=新的FileReader(配置文件);
如果(收入您的财产尚未加载。请参阅
您的代码应该如下所示:
File configFile=新文件(“intput.properties”);
Properties prop=新属性();
试一试{
FileReader read=新的FileReader(配置文件);
道具装载(读取);
//当您的收入始终>11000时,不会执行if块。
if(income下面的代码可能会有所帮助。可能是属性文件的位置或加载方法导致了问题
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();
}
}
}
您没有收到任何错误-因为您捕获并忽略了它们…将printStacktrace
放在每个catch
中,看看问题是什么…在发布代码时,您应该花时间格式化代码,尤其是缩进。您怎么能期望我们阅读如此糟糕的格式化代码?@rama:(,不可能,您的input.properties
的内容是什么?它在input.properties
中包含一个键tax
?#input.properties tax=0 NI=0 tax1=0.20 NI1=0.12 tax2=0.40 NI2=0.2 tax3=0。45@rama也许你需要从一个小例子开始。你的收入
没有通读道具
。我会做的也许是密码或什么的。
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); }
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();
}
}
}