【问题标题】:Why can't I read an Integer in my Properties file?为什么我不能在我的属性文件中读取整数?
【发布时间】:2012-10-03 05:22:13
【问题描述】:

我正在尝试用 Java 编写一个配置文件,并将我的端口号放入其中以便我的 HTTP Web 服务器连接到根路径。

配置文件:

root= some root
port=8020

我正在尝试访问这样的属性:

FileInputStream file = new FileInputStream("config.txt");       
//loading properties from properties file        
config.load(file);

int port = Integer.parseInt(config.getProperty("port"));   
System.out.println("this is port " + port);

如果我在 getProperty 方法中使用单个参数执行此操作,我会收到此错误

"java.lang.NumberFormatException: null"

但是,如果我这样访问它

int port = Integer.parseInt(config.getProperty("port", "80"));

它有效。

另外,它适用于config.getProperty("root");,所以我不明白...

编辑:

import java.net.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.*;

public class Server
{


    public static void main(String[] args) throws Exception
    {
        boolean listening = true;
        ServerSocket server = null;     
        Properties config = new Properties();
        int port = 0;
        try
        {              
            //Reading properties file
            FileInputStream file = new FileInputStream("config.txt");       
            //loading properties from properties file        
            config.load(file);

            port = Integer.parseInt(config.getProperty("port"));   
            System.out.println("this is port " + port);


            System.out.println("Server binding to port " + port);
            server = new ServerSocket(port);



        }
        catch(FileNotFoundException e)
        {
            System.out.println("File not found: " + e);
        }
        catch(Exception e)
        {
            System.out.println("Error: " + e);
            System.exit(1);
        }

        System.out.println("Server successfully binded to port " + port);

        while(listening)
        {
            System.out.println("Attempting to connect to client");
            Socket client = server.accept();
            System.out.println("Successfully connected to client");
            new HTTPThread(client, config).start();
        }

        server.close();
    }

}

【问题讨论】:

  • 你能提供一个独立的例子来重现你的问题吗?我怀疑当你重新检查你的测试时,你会发现 port 没有被加载。
  • OP,我们正在尝试帮助格式化代码(属性)。为什么要还原版本?顺便说一句this 可能会有所帮助。
  • 似乎config.txt中的“端口”属性不存在。
  • 哦,抱歉,我没注意到。我注意到了一些错误,所以我对其进行了编辑。
  • @JohnathanAu 把完整的代码放在这里,展示正确读取根目录和错误读取一次加载后的端口。

标签: java file-io properties


【解决方案1】:

您能否提供一个独立的示例来重现您的问题?

对不起,我不明白

当我跑步时

Properties prop = new Properties();
prop.setProperty("root", "some root");
prop.setProperty("port", "8020");
prop.store(new FileWriter("config.txt"), "test");

Properties config = new Properties();
//loading properties from properties file
config.load(new FileReader("config.txt"));

int port = Integer.parseInt(config.getProperty("port"));
System.out.println("this is port " + port);

我明白了

this is port 8020

【讨论】:

  • 我必须在我的程序中显式设置属性吗?我想自己在文本文件中输入...
  • @JohnathanAu Peter 刚刚测试并发现您的应用程序本身的阅读部分有效。您可以进行相同的测试并将上述测试的结果与您自己的道具文件进行比较。也许您发现了导致问题的不同之处?
  • @JohnathanAu 我这样做是为了展示如何编写一个任何人都可以运行的简单程序来证明存在或不存在问题。简而言之,您做错了什么,但您不知道它是什么,也没有足够的信息让我们告诉您它是什么。如果您构建了一个简单、完整的示例,您可能会自己找出问题所在,如果没有,您可以将其发布在这里,我们可以看到问题所在。
  • I want to type inside the text file myself... 在这种情况下,您需要正确编辑正确的文件,这似乎是您遇到的问题。
  • 我也认为你的 config.txt 中没有端口属性
【解决方案2】:

区别是

root= some root #STRING

port=8020  #INTEGER

所以要获得根属性,你可以这样做..

props.getProperty("root"); //Returns a String

对于整数

props.get("port"); //Returns a Object

Java Properties 类的工作原理

public String getProperty(String key) {
    Object oval = super.get(key);
    String sval = (oval instanceof String) ? (String)oval : null;
    return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval;
    }

//顺便说一句。 Peter Lawrey 将端口作为字符串 - 这就是为什么它在他的版本中有效。

阅读: difference between get & getProperty

【讨论】:

    【解决方案3】:
    properties.get("key) - should work for any object type - convert it later to a specific type
    properties.getProperty("key") -- ll always get a string regardless of a value type in the properties.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多