Files.exists() 判斷文件是否存在
Files.createFile() 創(chuàng)建文件
Files.createDirectory() 創(chuàng)建文件夾
Files.delete() 刪除文件
Files.copy() 復(fù)制文件
Files.move() 移動(dòng)文件
Files.size() 查看文件個(gè)數(shù)
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;
? ? }
}