0

I am loading property from file, the property contains path (Windows path) and I need to normalize it to create usable path. The problem is that I can't replace "\".

Here is my test class:

public class PathUtil {

    public static String normalizeEscapeChars(String source) {
        String result = source;

        result = result.replace("\b", "/b");
        result = result.replace("\f", "/f");
        result = result.replace("\n", "/n");
        result = result.replace("\r", "/r");
        result = result.replace("\t", "/t");
        result = result.replace("\\", "/");
        result = result.replace("\"", "/\"");
        result = result.replace("\'", "/'");
        return result;
    }

    public static void main(String[] args) {

        try(FileInputStream input = new FileInputStream("C:\\Users\\Rakieta\\Desktop\\aaa.properties")) {
            Properties prop = new Properties();
            prop.load(input);
            System.out.println(PathUtil.normalizeEscapeChars(prop.getProperty("aaa")));

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Here property file:

aaa=Intermix\koza , intermix\trace

Actual output is :

Intermixkoza , intermix/trace

Needed output is :

Intermix/koza , intermix/trace

Any suggestions?

XwOku
  • 3
  • 3

3 Answers3

1

When I copied your code my IDE threw an error saying \k is not a valid escape character. So I removed the whole line.

result = result.replace("\k", "/k");
// I have not seen that escape character (Correct me if I am wrong)

And my output was

aaa=Intermix/koza , intermix/trace

or you try what Connor said that is

result = result.replace("\\k", "/k");
// This code is replacing \k with /k in Intermix\koza. So it is kinda hard coded.

which also gives the same result.

  • Updated srr i left that "\k" and forgot to delete before upload. The problem is that i cant limit it to just "k" it has to work for any char. Double \ is not working it cannot convert "\" form file automatically to "\\". – XwOku Jul 25 '19 at 16:07
  • And btw how you got output of whole property file? when you are reading only the value of property? – XwOku Jul 25 '19 at 16:12
0

Use double backslash \\ to escape a backslash in java.

Connor
  • 365
  • 3
  • 10
  • I know that, but that property file gonna be customizable by client and I don’t want to force them to to think about it – XwOku Jul 25 '19 at 15:03
0

The backslash is already interpreted by the java.util.Properties class.

To bypass this, you can extend it and tweak the load(InputStream) method as shown in this answer:

public class PropertiesEx extends Properties {
    public void load(FileInputStream fis) throws IOException {
        Scanner in = new Scanner(fis);
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        while(in.hasNext()) {
            out.write(in.nextLine().replace("\\","\\\\").getBytes());
            out.write("\n".getBytes());
        }

        InputStream is = new ByteArrayInputStream(out.toByteArray());
        super.load(is);
    }
}
Izruo
  • 2,246
  • 1
  • 11
  • 23