java - 为什么我不能在我的 Properties 文件中读取 Integer?

标签 java file-io properties

我正在尝试用 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();
    }

}

最佳答案

Can you provide a self contained example to reproduce your problem?

Sorry, I don't understand

当我运行时

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

关于java - 为什么我不能在我的 Properties 文件中读取 Integer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12858299/

相关文章:

java - 从 CSV 文件 Java 清除回车符

java - FileWriter() 只会追加,不会覆盖

ios - 写入文件 :atomically: not saving new plist data

c++ - 将数据从 C++ Vector 快速写入文本文件

iphone - Objective C - 类别中的静态变量

ios - 如何在使用属性时修复此泄漏?

java - java.util.Set API 中可能存在的错误

java - 在数据流模板中调用 waitUntilFinish() 后可以运行代码吗?

c# - 属性、索引器或动态成员访问不能作为 out 或 ref 参数传递?

java - 使用 keystore 中的 SecretKeyEntry 解密加密字符串