0

i already read related post about reading a.config. The strategy is to use property, and method getproperty().

But what if i only have values there? how can i extract all value out? for example:

server1 127.0.0.1 50000
server2 127.0.0.1 50001
server3 127.0.0.1 50002
server4 127.0.0.1 50003
server5 127.0.0.1 50004
server6 127.0.0.1 50005
server7 127.0.0.1 50006
server8 127.0.0.1 50007

I wanted to read the whole file as a stringbuilder and parse it, but i am not sure if there is any other better way to process this config file.

Sandeep Singh
  • 745
  • 4
  • 20
johnway yang
  • 11
  • 1
  • 2
  • How about reading the file line by line using `BufferedReader`? A config file is nothing more than a simple file. If you are using a framework, then the framework might provide you some specialized classes for reading the file. Otherwise use simple `BufferedReader` or one of Apache commons utils. – Prashant Mar 09 '18 at 08:51
  • What Prashant says and then use String.split ... – Fildor Mar 09 '18 at 08:52
  • https://stackoverflow.com/questions/4677411/iterating-over-the-content-of-a-text-file-line-by-line-is-there-a-best-practic – Luk Mar 09 '18 at 08:55

3 Answers3

1

You can also use org.apache.commons.io.FileUtils,

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;

public class SomeClass 
{

    public static void main(String argp[]) throws Exception
    {
        List<String> lines = FileUtils.readLines(new File("d:/temp/config.txt")) ;

        List<Config> configs= new ArrayList<Config>() ;
        for(String line:lines)
        {
            line = line.trim();

            if(line.equals(""))
                    continue ;

            Config config= new Config() ;
            String[] values= line.split(" ");
            config.server = values[0] ;
            config.ip = values[1] ;
            config.ip = values[2] ;         
            configs.add(config) ;
        }

        for(Config config:configs)
            System.out.println(config) ;
    }

    public static class Config
    {
        public String server = "" ;
        public String ip = "" ;
        public String port = "" ;

        @Override
        public String toString() 
        {
            return "config [server=" + server + ", ip=" + ip + ", port=" + port
                    + "]";
        }               
    }
}
0

It seems you plan is to read this file and make a complex object with a serverid ipaddress and port?

If so there are hundreds of examples online for reading a file

    String fileName = "c://lines.txt";

    //read file into stream, try-with-resources
    String[] valueSplit;
    List<SomeObject> list = new ArrayList<>();
    try (Stream<String> stream = Files.lines(Paths.get(fileName))) {

        valueSplit = stream.split(" ");
        list.add(new SomeObject(valueSplit[0],valueSplit[1], valueSplit[2]);


    } catch (IOException e) {
        e.printStackTrace();
    }
GSUgambit
  • 4,459
  • 6
  • 25
  • 31
  • 1
    I would have ideally created a `<>` that returns an instance of a class that represents a configuration line and then used it for mapping each line with `SomeObject`. But this works too. :D – Prashant Mar 09 '18 at 09:01
  • @Prashant definitely agreed but from the context of the question, I assumed he may not be using Spring – GSUgambit Mar 09 '18 at 09:04
0

You can make use of .properties file type. In this you can declare the field and later extract them with the .getProperty() method.

With this you don't have to worry about parsing the content.

Example

File content of config.properties

server1=127.0.0.1:50000
server2=127.0.0.1:50001
server3=127.0.0.1:50002
server4=127.0.0.1:50003
server5=127.0.0.1:50004
server6=127.0.0.1:50005
server7=127.0.0.1:50006
server8=127.0.0.1:50007

java code to retrieve

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Test {

    public static void main(String[] args) {
        Properties config = new Properties();
        try {
            InputStream input = new FileInputStream(new File("path/to/file"));
            config.load(input);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(config.getProperty("server1"));
        System.out.println(config.getProperty("server2"));
        System.out.println(config.getProperty("server3"));
        System.out.println(config.getProperty("server4"));
        System.out.println(config.getProperty("server5"));
        System.out.println(config.getProperty("server6"));
        System.out.println(config.getProperty("server7"));
        System.out.println(config.getProperty("server8"));

    }
}

OUTPUT:

127.0.0.1:50000
127.0.0.1:50001
127.0.0.1:50002
127.0.0.1:50003
127.0.0.1:50004
127.0.0.1:50005
127.0.0.1:50006
127.0.0.1:50007
Saif Ahmad
  • 1,118
  • 1
  • 8
  • 24
  • The OP has specifically mentioned that the config file does not have key value pairs. Moreover, your approach requires the knowledge of the number of servers. When I look at a config file, as provided by OP, I get a feeling that the code in question, might not have any idea of the keys at all. `server1` only seems to be a simplified key. The actual server name could be quite complex. This approach might not be the most optimal solution. – Prashant Mar 09 '18 at 09:12
  • In my approach server name can be complex but we'll still be able to fetch the value. – Saif Ahmad Mar 09 '18 at 09:16