How to address cross-platform application in Java (OSX and Windows)

Saint Nick

Lifer
Jan 21, 2005
17,722
6
81
I am working on porting a current project over to Java so that our client is functional on Windows and OSX. My question is -- how do I address differences between the operating systems, namely, the file structure? I coded a client that behaves similarly to Dropbox. I'd like to keep the logic and code as clean as possible, but the system folder and file paths are throwing me for a loop.

Am I going to have to put a flag of sorts into the app to designate what OS it is on, and assign paths appropriately? Any tips here are appreciated. There are other differences that I may not be figuring in.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
In general the way I tackle this problem is to pick one style of file paths and program all of your logic using those constructs. Only when it is time to actually get a handle to the file or do some filesystem manipulation will I convert the file paths into the format required for the operating system the app is running on.

This way you write your file manipulation logic once, and then use a simple encoding/decoding pattern to translate paths during runtime.
 

beginner99

Diamond Member
Jun 2, 2009
5,320
1,767
136
On windows using "\" or "/" as file separator works without any issue including relative path.
 

Cogman

Lifer
Sep 19, 2000
10,286
147
106
Java 7, in particular, handles paths pretty painlessly. The Paths helper class should handle all OS differences for you.
 

Saint Nick

Lifer
Jan 21, 2005
17,722
6
81
Java 7, in particular, handles paths pretty painlessly. The Paths helper class should handle all OS differences for you.

Cool. I'm a little behind on current Java developments, and I think 7 is pretty recent. Thanks to you and everyone ITT.
 

smackababy

Lifer
Oct 30, 2008
27,024
79
86
Java 6 is EOL. But then, it might not be relevant for you.

I think a lot of enterprise is still on Java 6 (I know I have been for awhile). Sadly, we can't switch until we get approved to move to WebSphere 8, because 7 doesn't support Java 7 IIRC.


As far as the OP, if you can't use any of the Java 7 file path enhancements, write it yourself. Have the actual file manipulations code agnostic of the file system.
 
Sep 29, 2004
18,656
68
91
System.getParameter("file.separator") as somoene metnioned already is an important aspect.

As for Java 7 fixing things, be careful. Java does not change behavior even if something is done incorrectly. They keep the undesirable funcitonality so things don't break in later versions of Java.

My best advice is to read up on URLs. It's amazing what you can learn. Like a trialing slash defines a folder while no trailing slash means file:
Folder example. C:/temp/readme.txt/
File example: C:/temp

Yup, readme.txt above is a folder. And temp is a file.

What are your exact concerns?