Hi everyone,

I have a rather silly question but please bear with me for a while. If lets say i have a string which has the contents

"C:\\p\\kol\\pki.txt"

the thing with the above string is that if i use as an argument in a
fileinputstream it will only work on windows platform.

Basically my question is how do i convert the above string to a platform independent form.

I hope someone can help me with this

Thank You

Yours Sincerely

Richard West

Dani AI

Generated

Hard-coded Windows paths (drive letters, backslashes) are not convertible into a single “platform‑independent” absolute path. The practical fix is to stop embedding OS-specific literals and let Java build the path for the current platform at runtime.

Prefer the java.nio APIs to compose and open files; they join path segments using the platform separator and avoid string fiddling. For example:

Path p = Paths.get(System.getProperty("user.home"), "p", "kol", "pki.txt");
try (InputStream in = Files.newInputStream(p)) {
    // read from stream
}

This avoids hard-coded drive letters and makes the code portable between Windows, Linux and macOS. For files shipped inside the application, use the classloader (getResourceAsStream) so the file is loaded from the classpath instead of a filesystem path.

A couple of clarifications that close gaps in the thread: getAbsolutePath() and getCanonicalPath() turn a File into an absolute filesystem string on the current OS — canonical also normalizes and resolves symlinks — but neither will translate a Windows drive path into a Linux path. Also, the property for the file separator is file.separator; path.separator is the separator used in PATH-like lists (":" or ";").

Practical checklist: avoid absolute OS literals, store a configurable base directory (or use user.home/classpath), build paths with Paths.get(...) or File constructors that take parent + child, and prefer forward slashes or the java APIs when interacting purely with the JVM. This follows ’s point about relative/configurable paths and supplements and with concrete, modern APIs and the correct property names.

Recommended Answers

All 4 Replies

well, as it uses a drive letter it will ever only work on Windows...
That's the problem with files, path structures are platform dependent.

The best you can do is to either use relative paths only so you will always look from your installation directory, in which case you can use the system properties to get the path separator (path.separator system property) to figure out whether to use / or \, or else to use a configuration file which contains the filenames and paths. You can make this a file unique to each platform or (this should be possible) detect the platform you're running on and select the single correct entry for that platform from the config file based on that.

As config files can be stored and read using Java classpath entries through ResourceBundle there's no need to know the exact path specifiers for them.

Hi everyone,

Can i do this to create a platform independent file

String str1 = "C:\\abc\\efg.exe"
     File file1 = new File(str1);
     String str2 = file1.getCanonicalPath();
     //Now i will the instance of str2 for any file streams

OR THIS

String str1 = "C:\\abc\\efg.exe"
     File file1 = new File(str1);
     String str2 = file1.getAbsolutePath();
     //Now i will the instance of str2 for any file streams

Will what i do create a platform independent file?

By the way

Merry Christmas and a Happy New Year to everyone

Thank You

Yours Sincerely

Richard West

Hi everyone,

Can i do this to create a platform independent file

String str1 = "C:\\abc\\efg.exe"
     File file1 = new File(str1);
     String str2 = file1.getCanonicalPath();
     //Now i will the instance of str2 for any file streams

OR THIS

String str1 = "C:\\abc\\efg.exe"
     File file1 = new File(str1);
     String str2 = file1.getAbsolutePath();
     //Now i will the instance of str2 for any file streams

Will what i do create a platform independent file?

By the way

Merry Christmas and a Happy New Year to everyone

Thank You

Yours Sincerely

Richard West

u can use some ppty files to take the location an can configure
on dat about location.

dat's is the only possible way to make platform independent

for ex:. u can take systemroot as root
in windows it may be c:\windows and
in linux it will be "/" based on this u can take the relative path.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.