0

I have a config File "Replace_IP_List.cfg" as below

10.19.120.39=[fec0:1111:2222:3333:4444]
[fec0:2222:2222:3333:4444] = 10.18.215.151

I am reading cfg file to get keys and values, since the key has ':' , i am not able to get full string,

Properties prop = new Properties();
    InputStream is = new FileInputStream("Replace_IP_List.cfg");

    prop.load(is);
    Enumeration<?> e = prop.propertyNames();
    while (e.hasMoreElements())
    {
        String key = (String) e.nextElement();
        String value = prop.getProperty(key);
        System.out.println("Key : " + key + ", Value : " + value);
        str=str.replace(key, value);

output:

Key : 10.19.120.39, Value : [fec0:1111:2222:3333:4444]
Key : [fec0, Value : 2222:2222:3333:4444] = 10.18.215.151

the cfg file is generated runtime(or it is provided by others) i donot need to edit the cfg file or escape as :

can anyone suggest me Inside program how i can escape : and consider only = while reading the file. Expected output as below,

Key : 10.19.120.39, Value : [fec0:1111:2222:3333:4444]
Key : [fec0:2222:2222:3333:4444], Value : 10.18.215.151
Sunil Kumar
  • 367
  • 2
  • 5
  • 19

1 Answers1

0

One approach may be cloning your property file while escaping colons preceding to equal signs and reading that property file in a helper class like:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.util.Enumeration;
import java.util.Properties;

public class PropertyFileHelper {

    class FakeReader extends Reader {

        private Reader in;
        private File file;

        public FakeReader(Reader in) throws IOException {

            file = File.createTempFile("dummy", "dummuy");

            try(
                    BufferedReader r = new BufferedReader(in);
                    BufferedWriter w = new BufferedWriter(new FileWriter(file));
                    ){

                String line;
                while((line = r.readLine()) != null) {
                    if(line.contains(":") && line.contains("=") && line.indexOf(':') < line.indexOf('=')){
                        String modifiedLine = "";
                        boolean equalFetched = false;
                        for(char c : line.toCharArray()) {
                            modifiedLine = modifiedLine.concat(equalFetched ? "" : ( c == ':' ? "\\" : "") + c) ;
                            equalFetched = c == '=';
                        }
                        line = modifiedLine;
                    }
                    w.write(line);
                    w.newLine();
                }
            }
            this.in = new FileReader(file);
        }

        @Override
        public int read(char[] cbuf, int off, int len) throws IOException {
            return in.read(cbuf, off, len);
        }

        @Override
        public void close() throws IOException {
            this.in.close();
        }

    }

    private void test() throws IOException {

        Properties prop = new Properties();

        prop.load(new FakeReader(new FileReader("/home/guest/Desktop/p.properties")));

        Enumeration<?> e = prop.propertyNames();
        while (e.hasMoreElements())
        {
            String key = (String) e.nextElement();
            String value = prop.getProperty(key);
            System.out.println("Key : " + key + " Value : " + value);
        }

    }

    public static void main(String[] args) throws IOException {
        new PropertyFileHelper().test();
    }

}
guleryuz
  • 2,714
  • 1
  • 15
  • 19