How do I programmatically change file permissions?
Full control over file attributes is available in Java 7, as part of the "new" New IO facility (NIO.2). For example, POSIX permissions can be set on an existing file with setPosixFilePermissions()
, or atomically at file creation with methods like createFile()
or newByteChannel()
.
You can create a set of permissions using EnumSet.of()
, but the helper method PosixFilePermissions.fromString()
will uses a conventional format that will be more readable to many developers. For APIs that accept a FileAttribute
, you can wrap the set of permissions with with PosixFilePermissions.asFileAttribute()
.
Set<PosixFilePermission> ownerWritable = PosixFilePermissions.fromString("rw-r--r--");
FileAttribute<?> permissions = PosixFilePermissions.asFileAttribute(ownerWritable);
Files.createFile(path, permissions);
In earlier versions of Java, using native code of your own, or exec
-ing command-line utilities are common approaches.
java- How do programmatically change user to root before changing file permissions?
There is no way to change the owner of a running process (be it a JVM or anything else).
You can however spawn a new, privileged process to do the "dirty" work.
How do I programmatically change file permissions?
Full control over file attributes is available in Java 7, as part of the "new" New IO facility (NIO.2). For example, POSIX permissions can be set on an existing file with setPosixFilePermissions()
, or atomically at file creation with methods like createFile()
or newByteChannel()
.
You can create a set of permissions using EnumSet.of()
, but the helper method PosixFilePermissions.fromString()
will uses a conventional format that will be more readable to many developers. For APIs that accept a FileAttribute
, you can wrap the set of permissions with with PosixFilePermissions.asFileAttribute()
.
Set<PosixFilePermission> ownerWritable = PosixFilePermissions.fromString("rw-r--r--");
FileAttribute<?> permissions = PosixFilePermissions.asFileAttribute(ownerWritable);
Files.createFile(path, permissions);
In earlier versions of Java, using native code of your own, or exec
-ing command-line utilities are common approaches.
set execute permition on file
JAVA5
and prior there is workaround using exec
extracted from here:
public static void runCmd (String[] cmd) {
try {
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader r = new BufferedReader(
new InputStreamReader (
p.getInputStream()
)
);
} catch(Exception e) {
}
}
USE
runCmd(new String[] {
"/bin/chmod",
"755",
"/path/to/your/script"
});
By terminal in Linux
UP TO JAVA6
For permissions use File::setExecutable(boolean [, boolean])
File file = new File("/your/path/to/file/file_script.sh");
file.setExecutable(true);
Check this MKYONG permissions tutorial
Set File Permissions in C#
Take a look at File.SetAttributes(). There are lots of examples online about how to use it.
Taken from that MSDN page:
FileAttributes attributes = File.GetAttributes(path);
if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
{
// Show the file.
attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
File.SetAttributes(path, attributes);
Console.WriteLine("The {0} file is no longer hidden.", path);
}
else
{
// Hide the file.
File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
Console.WriteLine("The {0} file is now hidden.", path);
}
Related Topics
Byte Order Mark Screws Up File Reading in Java
How to Secure an API Rest For Mobile App (If Sniffing Requests Gives You the "Key")
Get Type of a Generic Parameter in Java With Reflection
Get a List of Resources from Classpath Directory
How Are Ssl Certificate Server Names Resolved/How to Add Alternative Names Using Keytool
Java Error: Comparison Method Violates Its General Contract
Difference Between Jdk and Jre
Getting the Name of the Currently Executing Method
Properly Removing an Integer from a List≪Integer≫
Java.Sql.Sqlexception Parameter Index Out of Range (1 ≫ Number of Parameters, Which Is 0)
How to Convert Milliseconds to "X Mins, X Seconds" in Java
How to Read from a Inputstream With a Timeout
Maven: Add a Dependency to a Jar by Relative Path
Unicode Equivalents For \W and \B in Java Regular Expressions