为什么我不能在属性文件中读取整数?

问题描述 投票:10回答:3

我正在尝试用Java编写一个configs文件,并将我的端口号放在其中,以便我的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();
    }

}
java file-io properties
3个回答
16
投票

你能提供一个自包含的例子来重现你的问题吗?

对不起,我不明白

我跑的时候

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

7
投票

不同的是

root= some root #STRING

port=8020  #INTEGER

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

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;
    }

// BTW。 Peter Lawrey将端口视为字符串 - 这就是为什么它的作品在他的版本中。

阅读本文:difference between get & getProperty


0
投票
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.
© www.soinside.com 2019 - 2024. All rights reserved.