javaurlurldecode

URL encoding, white spaces and special characters (e.g.: "+"). How to properly manage file paths?


I've been reading tons of Stack Overflow posts about Java path encodings and proper ways to manage file paths (like this one). Still, I can't really get how to manage my file path.

What I want to achieve it's pretty straightforward: get my *.jar file path encoded in a proper way so that I may be able to use it as an input to FileInputStream(): in this way I can load a properties file which is located in the same folder of the aforementioned *.jar.

I've read Java documentation and I understood that URLDecoder just escapes special symbols (like "+") with blank spaces, so I can't really get which methods combination I've to use to get my absolute path to the file, even if its containing folders (not my fault) names' are made of white spaces and said symbols. This is my method:

private FileInputStream loadInputPropertiesFile() {
    FileInputStream input = null;

    // Let's load the properties file from the *.jar file parent folder.
    File jarPath = new File(PropertiesManagement.class.getProtectionDomain().getCodeSource().getLocation().getPath());
    String propertiesPath = String.format("%s/", jarPath.getParentFile().getAbsolutePath());

    propertiesPath = URLDecoder.decode(propertiesPath, StandardCharsets.UTF_8);

    // This part has been added just for tests, to better understand how file paths were encoded.
    try {
        URL url = jarPath.toURI().toURL();
        File path = Paths.get(url.toURI()).toFile();
        System.out.println(path);
    } catch (MalformedURLException | URISyntaxException e) {
        e.printStackTrace();
    }

    try {
        input = new FileInputStream(propertiesPath + CONFIG_FILE_PATH);
    } catch (FileNotFoundException e) {
        System.out.println("Properties file not found! Have you deleted it?");
        e.printStackTrace();
    }

    return input;
}

EDIT
The file path I'd like to get is something like this: "C:\Some + Folder\x64".
In the end, the returned input should be something like this: "C:\Some + Folder\x64\config.properties".


Solution

  • In the end I fixed it and it's working for paths with special symbols, too.
    Hope it will help someone having my same issue.

    /**
     * It loads the 'config.properties' file into a FileInputStream object.
     *
     * @return The FileInputStream object.
     */
    private FileInputStream loadInputPropertiesFile() {
        FileInputStream input = null;
    
        // Let's load the properties file from the *.jar file parent folder...
        File jarPath = new File(PropertiesManagement.class.getProtectionDomain().getCodeSource().getLocation().getPath());
        String propertiesPath = String.format("%s" + File.separator, jarPath.getParentFile().getAbsolutePath());
        propertiesPath = propertiesPath.replace("%20", "\u0020");
    
        try {   
            // ... and setting the pointer to the same path, ready to read the properties file.
            input = new FileInputStream(propertiesPath + CONFIG_FILE_PATH);
        } catch (FileNotFoundException e) {
            System.out.println("Properties file not found! Have you deleted it?");
            e.printStackTrace();
        }
    
        return input;
    }