10

I'm trying to write a configs file in Java and I put my port number inside it for my HTTP Web Server to connect to and also the root path.

Configs file:

root= some root
port=8020

I am trying to access the properties like this:

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 it with a single parameter in the getProperty method, I get this error

"java.lang.NumberFormatException: null"

However, if I access it like this

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

it works.

Also, 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();
    }

}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
Johnathan Au
  • 5,244
  • 18
  • 70
  • 128
  • 5
    Can you provide a self contained example to reproduce your problem? I suspect when you re-examine your test you will find that the `port` was not being loaded. – Peter Lawrey Oct 12 '12 at 11:44
  • OP, we're trying to help format the code (properties). Why do you revert the editions ? BTW [this](http://stackoverflow.com/editing-help) might help. – Denys Séguret Oct 12 '12 at 11:45
  • it seems that in config.txt the property "port" does not exist. – gefei Oct 12 '12 at 11:45
  • Oh sorry, I didn't notice. I noticed some mistakes so I edited it. – Johnathan Au Oct 12 '12 at 11:46
  • @JohnathanAu Put here the complete code demonstrating the correct read of root and the incorrect read of port after one load. – Denys Séguret Oct 12 '12 at 11:46
  • Is the result of the 2-Param-`getProperty` = 80 by any chance? – Fildor Oct 12 '12 at 11:49
  • @JohnathanAu hey man, how did you solve this? i am having the same problem retrieving and int parameter from my confing file. It wont cast from Object to int...so i am having trouble getting my port number. thanks – caniaskyouaquestion Dec 09 '14 at 10:48

3 Answers3

15

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

Sorry, I don't understand

When I run

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
Community
  • 1
  • 1
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Do I have to explicitly setProperty in my program? I want to type inside the text file myself... – Johnathan Au Oct 12 '12 at 11:52
  • @JohnathanAu Peter just tested and found the reading part of your app itself works. You can do the same test and diff the outcome of above test and your own props-file. Maybe you find a difference causing your problem? – Fildor Oct 12 '12 at 11:53
  • 1
    @JohnathanAu I did this to show how you can write a simple program which anyone can run to demonstrate there is or is not a problem. In short, you are doing something wrong, but you don't know what it is and there isn't enough information for us to tell you what it is. If you construct a simple, complete example you are likely to work out what the problem is yourself, if not you can post it here and we can see what the matter is. – Peter Lawrey Oct 12 '12 at 11:55
  • `I want to type inside the text file myself...` In that case you need to edit the right file correctly which is what you appear to be having trouble with. – Peter Lawrey Oct 12 '12 at 11:57
  • 1
    I also think there is not port property in your config.txt – Giulio Quaresima Oct 12 '12 at 11:58
  • I bet there is some formatting issue that prevents the port-property to be recognized by java. – Fildor Oct 12 '12 at 11:59
7

The difference is

root= some root #STRING

port=8020  #INTEGER

so to get the root property you can do this..

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

for the integer

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

How the Java Properties 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;
    }

//btw. Peter Lawrey is putting the port as String - thats why its works in his version.

READ THIS: difference between get & getProperty

Community
  • 1
  • 1
Alexander Sidikov Pfeif
  • 2,418
  • 1
  • 20
  • 35
1
properties.get("key) - should work for any object type - convert it later to a specific type
properties.getProperty("key") -- will always get a string regardless of a value type in the properties.
Alexander Sidikov Pfeif
  • 2,418
  • 1
  • 20
  • 35
Pravin Bansal
  • 4,315
  • 1
  • 28
  • 19