files的方法

Files.exists() 判断文件是否存在

Files.createFile() 创建文件

Files.createDirectory() 创建文件夹

Files.delete() 删除文件

Files.copy() 复制文件

Files.move() 移动文件

Files.size() 查看文件个数

Files.read() 读取文件

Files.write() 写入文件




public final class Files {

    private Files() { }

  public static boolean exists(Path path, LinkOption... options) {

        try {

            if (followLinks(options)) {

                provider(path).checkAccess(path);

            } else {

                // attempt to read attributes without following links

                readAttributes(path, BasicFileAttributes.class,

                              LinkOption.NOFOLLOW_LINKS);

            }

            // file exists

            return true;

        } catch (IOException x) {

            // does not exist or unable to determine if file exists

            return false;

        }

    }

  public static Path createFile(Path path, FileAttribute<?>... attrs)

        throws IOException

    {

        EnumSet<StandardOpenOption> options =

            EnumSet.<StandardOpenOption>of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);

        newByteChannel(path, options, attrs).close();

        return path;

    }

  public static Path createDirectory(Path dir, FileAttribute<?>... attrs)

        throws IOException

    {

        provider(dir).createDirectory(dir, attrs);

        return dir;

    }

    public static void delete(Path path) throws IOException {

        provider(path).delete(path);

    }


    public static Path copy(Path source, Path target, CopyOption... options)

        throws IOException

    {

        FileSystemProvider provider = provider(source);

        if (provider(target) == provider) {

            // same provider

            provider.copy(source, target, options);

        } else {

            // different providers

            CopyMoveHelper.copyToForeignTarget(source, target, options);

        }

        return target;

    }


    public static Path move(Path source, Path target, CopyOption... options)

        throws IOException

    {

        FileSystemProvider provider = provider(source);

        if (provider(target) == provider) {

            // same provider

            provider.move(source, target, options);

        } else {

            // different providers

            CopyMoveHelper.moveToForeignTarget(source, target, options);

        }

        return target;

    }


    public static long size(Path path) throws IOException {

        return readAttributes(path, BasicFileAttributes.class).size();

    }

    private static byte[] read(InputStream source, int initialSize) throws IOException {

        int capacity = initialSize;

        byte[] buf = new byte[capacity];

        int nread = 0;

        int n;

        for (;;) {

            // read to EOF which may read more or less than initialSize (eg: file

            // is truncated while we are reading)

            while ((n = source.read(buf, nread, capacity - nread)) > 0)

                nread += n;

            // if last call to source.read() returned -1, we are done

            // otherwise, try to read one more byte; if that failed we're done too

            if (n < 0 || (n = source.read()) < 0)

                break;

            // one more byte was read; need to allocate a larger buffer

            if (capacity <= MAX_BUFFER_SIZE - capacity) {

                capacity = Math.max(capacity << 1, BUFFER_SIZE);

            } else {

                if (capacity == MAX_BUFFER_SIZE)

                    throw new OutOfMemoryError("Required array size too large");

                capacity = MAX_BUFFER_SIZE;

            }

            buf = Arrays.copyOf(buf, capacity);

            buf[nread++] = (byte)n;

        }

        return (capacity == nread) ? buf : Arrays.copyOf(buf, nread);

    }

    public static Path write(Path path, byte[] bytes, OpenOption... options)

        throws IOException

    {

        // ensure bytes is not null before opening file

        Objects.requireNonNull(bytes);

        try (OutputStream out = Files.newOutputStream(path, options)) {

            int len = bytes.length;

            int rem = len;

            while (rem > 0) {

                int n = Math.min(rem, BUFFER_SIZE);

                out.write(bytes, (len-rem), n);

                rem -= n;

            }

        }

        return path;

    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容