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

Pavan Kumar picture Pavan Kumar · May 8, 2015 · Viewed 62.5k times · Source
File f = new File("C:\\Temp\\Example.txt");
f.createNewFile();

On executing, a new file named "Example.txt" will be created in the Temp folder. How do I provide the file path in Mac OS X?

I tried providing:

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

But it didn't work for me.

Answer

Akash Rajbanshi picture Akash Rajbanshi · May 8, 2015

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

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