How do I provide a file path in Mac OS X while creating a file in Java?

為{幸葍}努か 提交于 2019-11-30 17:52:22

Forward slash "/" must be used to get the file path here. Use:

File f = new File("/Users/pavankumar/Desktop/Testing/Java.txt");
f.createNewFile();
Spindizzy

Please use File.separator to be independent from the OS:

String home = System.getProperty("user.home");
File f = new File(home + File.separator + "Desktop" + File.separator + "Testing" + File.separator + "Java.txt");

Or use org.apache.commons.io.FilenameUtils.normalize:

File f = new File(FileNameUtils.normalize(home + "/Desktop/Testing/Java.txt"));

Either of them can be used (the second option needs library)

There is a File.separator system-dependent constant that you should use to provide some portability to your Java code.

achedeuzot

On Linux, Mac OS X and other *nix flavours, the folder separator is / not \, so there isn't any need to escape anything, some/path/of/folders.

Also, you can use the /tmp folder for your temporary files.

Finally, on *nix systems, the home directory is usually represented by ~ or is in the environment variable HOME.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!