集册 Java实例教程 SeekableByteChannel和文件属性

SeekableByteChannel和文件属性

欢马劈雪     最近更新时间:2020-01-02 10:19:05

491
为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, 
展开阅读全文