簡體   English   中英

我無法在Java中顯示屬性文件的輸出

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM