为Unix和其他POSIX文件系统创建具有一组特定文件权限的文件。
import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.SeekableByteChannel; import java.nio.file.Files;/** from NowJava.com**/ import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.nio.file.attribute.FileAttribute; import java.nio.file.attribute.PosixFilePermission; import java.nio.file.attribute.PosixFilePermissions; import java.util.EnumSet; import java.util.Set; public class Main {//n o w j a v a . c o m 提供 public static void main(String[] args) { Path path = Paths.get("home/folder1/email", "email.txt"); ByteBuffer buffer = ByteBuffer.wrap("this is a test ".getBytes()); // create the custom permissions attribute for the email.txt file Set<PosixFilePermission> perms = PosixFilePermissions .fromString("rw-r------"); FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions .asFileAttribute(perms); // write a file using SeekableByteChannel try (SeekableByteChannel seekableByteChannel = Files.newByteChannel(path, EnumSet.of(StandardOpenOption.CREATE,