简体   繁体   English

我无法在Java中显示属性文件的输出

[英]I cant display the output from properties file in java

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"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM