File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes Cross Platform File Path Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Cross Platform File Path" Watch "Cross Platform File Path" New topic

Cross Platform File Path

Justin Nauman

Joined: Feb 25, 2009
Posts: 3
Hello all,
I am currently tasked with writing a service method that abstracts away some of the details about how a file that is uploaded to our web application is stored. The goal here is to allow us to upgrade to a CMS in the future and not have to change a great deal of code.

I admittedly have very little experience in the File I/O area, but the general idea is that our default implementation will be writing the files to a file system and storing the path to a DB, along with some other metadata that is necessary for the application's use.

The issue I could use some help on is: "what is the best way to store the 'path' to the file if our application needs to be deployed across mutliple operating systems?"

The development boxes for unit testing are on Windows, and the production environments sit on UNIX. The server file system itself will also be a Windows system that can be accessed via Unix or Windows. In Windows it would be using the typical UNC file naming conventions like: \\<SERVER_NAME>, but in Unix we have it set up so that there is a symbolic link to the same server at /<SERVER_NAME>.

I have considered just replacing all of the File.SeperatorChars with a REGEX with a character that is OS agnostic before saving it into the DB's FILE_LOCATION column, but this seems a big ugly. Are there any other methods that you have found to work well that I could investigate?

Thanks in advance for your help,
Justin N.
Chicago, IL
Rob Spoor

Joined: Oct 27, 2005
Posts: 20271

You can use / with Windows as well - the API is smart enough to convert it to \. So a UNC path of //server/share/folder/file will work just fine.

How To Ask Questions How To Answer Questions
Justin Nauman

Joined: Feb 25, 2009
Posts: 3
Thanks much! That seems to have done the trick. I didn't really think about what // in a Unix environment would "mean", but it references the root twice, which is no big deal.

Thanks again!
Justin N.
Chicago, IL
It is sorta covered in the JavaRanch Style Guide.
subject: Cross Platform File Path
It's not a secret anymore!