Warrior990
Warrior990

Reputation: 69

Reading path files without using extra backslash

I have a .ini file that looks like this and am using ini4j to parse the file:

[Filepath]
Inbound=C:\Users\Bilbo\Desktop\Testing

I want to return that exact string (C:\Users\Bilbo\Desktop\Testing) and I have the following java code:

public static String ParseIniInbound (File iniFile) throws 
InvalidFileFormatException, IOException {
    String iniFileName = iniFile.toString();
    Ini ini = new Ini(new File(iniFileName));
    String InboundPath= ini.get("Filepath", "Inbound");
    return InboundPath;
}

However, what is returned is C:UsersBilboDesktopTesting

I tried putting quotes around the filepath in the .ini file to read it as a string but that didn't work. I used double slashes (C:\\Users\\Bilbo\\Desktop\\Testing) in the .ini file which returns what im looking for (C:\Users\Bilbo\Desktop\Testing) but I want to be able to just copy and paste a filepath and not have to manually put in double slashes in the .ini flie. Is there a way to read in a string from an .ini file with ini4j or another way around this? Thanks

Upvotes: 0

Views: 776

Answers (2)

peterulb
peterulb

Reputation: 2988

Easiest way would be to disable escaping.

import org.ini4j.Config;
...
Config.getGlobal().setEscape(false);

Other alternative would be to use the Wini class instead

import org.ini4j.Wini;
...
Wini ini = new Wini(new File(iniFileName));

Upvotes: 1

Rafael Palomino
Rafael Palomino

Reputation: 318

Just change slash for double slash after reading the file path:

public static String ParseIniInbound (File iniFile) throws 
InvalidFileFormatException, IOException {
    String iniFileName = iniFile.toString().replaceAll("\\", "\\\\");
    Ini ini = new Ini(new File(iniFileName));
    String InboundPath= ini.get("Filepath", "Inbound");
    return InboundPath;
}

Upvotes: 0

Related Questions