Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading path from ini file, backslash escape character disappearing?

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.

like image 757
user2521350 Avatar asked Jul 28 '14 18:07

user2521350


1 Answers

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("\\\\", "\\\\\\\\");
like image 166
grkvlt Avatar answered Nov 17 '22 13:11

grkvlt