Why can't I read Integer in my properties file?

I am trying to write a config file in Java, and I put my port number in it so that the HTTP HTTP server can connect to the root path as well as the root path.

Configures the file:

root= some root port=8020 

I am trying to access the following properties:

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

If I do this with a single parameter in the getProperty method, I get this error

 "java.lang.NumberFormatException: null" 

However, if I refer to it like this:

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

it works.

In addition, it works for config.getProperty("root"); so i don't understand ...

Edit:

 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(); } } 
+6
source share
2 answers

Can you provide a standalone example to reproduce your problem?

Sorry I do not understand

When i started

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

I get

 this is port 8020 
+12
source

Difference

 root= some root #STRING port=8020 #INTEGER 

to get the root property you can do this.

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

for an integer

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

How the Java Property Class Works

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

// by the way. Peter Laurie puts the port as a String - that's why his work in his version.

READ THIS: the difference between get and getProperty

+5
source

Source: https://habr.com/ru/post/927561/


All Articles