views:

31

answers:

2

hi. am writing a java code that needs to save a file.

the below code is for mac.

ImageIO.write(movie_image, "jpg",new File("/Users/sathyap/Desktop/movieimages/"+fileName+".jpg"));

is there a way i can give the directory structure "/Users/sathyap/Desktop/movieimages/" hardcoded that works for both mac and windows.

A: 

EDIT: Totally changing my answer because I think what you're looking for is sticking to cross-platform rather than coding for each situation separately.

System.getProperty("user.home")
McAden
hey. thanks real useful. will come in handy.
codesburner
A: 

Assuming that the program runs under a directory (X) in either Mac and Windows. You can code it like this:

//This will give the current working directory
String directoryPath = System.getProperty("user.dir");

//now get the platform dependent path
String filePath = directoryPath + File.separator + "data" + File.separator + "filename";

File fileToWriteTo = new File(filePath);

File.separator will take care of platform ... your files will always be saved under current working direcoty ... you can provide shortcuts to access those paths and place them on desktop

If a user can execute the program that writes to that directory ... I will assume that the user can also access the files without problem

jsshah
thanks. works great. "If a user can execute the program that writes to that directory ... I will assume that the user can also access the files without problem" thats true.
codesburner
This won't work in Windows if your program is installed in Program Files. It expects you to store post-install files under the user. user.dir is the working directory.
McAden