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.

4

2 回答 2

9

尝试在 Ini4j 中将转义属性设置为 false。

你可以试试:

Config.getGlobal().setEscape(false);
于 2014-07-28T18:34:00.480 回答
2

如果您阅读文件然后将其转换\/处理前的文件,那将起作用。所以你正在使用的库有一个Ini#load(InputStream)获取 INI 文件内容的方法,这样调用它:

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

处理器必须对反斜杠进行解释,因此这将为它提供带有正斜杠的数据。或者,您可以在处理之前转义反斜杠,如下所示:

String contents = new String(data).replaceAll("\\\\", "\\\\\\\\");
于 2014-07-28T18:10:01.797 回答