5

I'm reading in an absolute pathname from an ini file and storing the pathname as a String value in my program. However, when I do this, the value that gets stored somehow seems to be losing the backslash so that the path just comes out one big jumbled mess? For example, the ini file would have key, value of:

key=C:\folder\folder2\filename.extension

and the value that gets stored is coming out as C:folderfolder2filename.extension.

Would anyone know how to escape the keys before it gets read in?

Let's also assume that changing the values of the ini file is not an alternative because it's not a file that I create.

user2521350
  • 85
  • 1
  • 9
  • share code for better & clear picture. – Braj Jul 28 '14 at 18:07
  • 1
    to have backslash, you need to escape it then "key=C:\\folder\\folder2\\filename.extension" will contains the backslash. – mpromonet Jul 28 '14 at 18:08
  • ^ The above would require that I go into the actual .ini file and change the value of the path. However, as I mentioned, this is not a feasible approach for me. – user2521350 Jul 28 '14 at 18:10
  • what are you using to parse the ini file? – jtahlborn Jul 28 '14 at 18:12
  • ini4j (http://ini4j.sourceforge.net/license.html), an api for parsing ini files – user2521350 Jul 28 '14 at 18:13
  • I also see that Ini4j has a config option for `PROP_ESCAPE` that is set to true by default. The documentation is terrible, so I have no idea how to change that, though... – grkvlt Jul 28 '14 at 18:32
  • See another discussion and answer on the same topic here:[the correct answer][1] [1]: http://stackoverflow.com/questions/30651304/inno-setup-5-ini4j-strips-backslash – Ed S Jun 05 '15 at 13:09

2 Answers2

10

Try setting the escape property to false in Ini4j.

You can try:

Config.getGlobal().setEscape(false);
user2521350
  • 85
  • 1
  • 9
grkvlt
  • 2,577
  • 1
  • 21
  • 38
  • I think the question I posed was very API-specific, because there is a very specific block of code I need to include in my program which will cause values with escape characters that get read in to not escape. That code is: Config.getGlobal().setEscape(false); Your response to my question, however, led me to this answer so it will be marked as the accepted answer. Thanks for your help! – user2521350 Jul 28 '14 at 18:51
  • 1
    No problem. I also tried using the same library and although the `replaceAll` did work, changing the config like that is a much better way to do it. – grkvlt Jul 28 '14 at 18:57
2

If you read the file and then translate the \ to a / before processing, that would work. So the library you are using has a method Ini#load(InputStream) that takes the INI file contents, call it like this:

byte[] data = Files.readAllBytes(Paths.get("directory", "file.ini");
String contents = new String(data).replaceAll("\\\\", "/");
InputStream stream = new ByteArrayInputStream(contents.getBytes());
ini.load(stream);

The processor must be doing the interpretation of the back-slashes, so this will give it data with forward-slashes instead. Or, you could escape the back-slashes before processing, like this:

String contents = new String(data).replaceAll("\\\\", "\\\\\\\\");
grkvlt
  • 2,577
  • 1
  • 21
  • 38
  • I'm still running into the same issue. The output string continues to 'escape' the backslash so that it doesn't show. Also, I'll point out that I had to use four backslashes "\\\\" for the replaceAll method since both Java and regex interpret the backslash as escape characters. Otherwise, I get an error using just "\\" – user2521350 Jul 28 '14 at 18:28
  • Did you try replacing backslash with forward slash? – grkvlt Jul 28 '14 at 18:31
  • Yes, I did. I think the root of the problem is that for some reason the backslash is never read into the value in the first place, so I don't think the replaceAll is actually doing anything – user2521350 Jul 28 '14 at 18:32
  • It will be. Make sure you are passing the new `InputStream` to the `ini.load()` method. Try calling `System.err.println(contents)` to verify the change, too. – grkvlt Jul 28 '14 at 18:36