After compiling the following code in Eclipse using a Mac:
import java.io.*;
public class Filer{
public static void main(String[] args) throws IOException{
File f1;
f1 = new File("/System/file.txt");
if(!f1.exists()){
f1.createNewFile();
}
}
}
I get an error:
Exception in thread "main" java.io.IOException: Permission denied
at java.io.UnixFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:883)
at Filer.main(Filer.java:11)
Can anyone tell me why that is? Is there any way to change the permissions? And if I were to compile this as a .jar file and send it to someone, would that person have the correct permissions?
Your user doesn’t have permission to create a file in that directory.
The same way you would change the permissions of any directory.
In Java 7+
I suspect the correct permissions for a directory called
/Systemis that you NOT have write access.Is there any reason not to use the home directory or the current working directory?