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

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.