java入门篇(36)Files工具类

it2022-05-05  212

文章目录

一、Files类1.1 简介1.2 常用方法1.3 重要方法二、测试2.1 列出文件指定目录下的所有文件和子目录2.2 判断c盘的总空间,可用空间 三、Paths,Path类3.1 简介3.2 常用方法 四、FileVisitor4.1 简介4.2 使用方法4.3 遍历输出指定目录下的所有文件夹和目录五、访问文件属性5.1常用的接口5.2 获取文件的创建时间

一、Files类

1.1 简介

操作文件和文件夹的工具类

1.2 常用方法

static Path copy(Path source, Path target, CopyOption… options) 将文件复制到目标文件。 path类可通过Paths.get(String src)获得。 static Path createDirectory(Path dir, FileAttribute<?>… attrs) 创建一个新的目录。 static Path createLink(Path link, Path existing) 为现有文件创建新的链接(目录条目) (可选操作) static void delete(Path path) 删除文件。 static Object getAttribute(Path path, String attribute, LinkOption… options) 读取文件属性的值。 Files.getAttribute(Paths.get("E:\\a.java"), "UNIX:uid"); static boolean isDirectory(Path path, LinkOption… options) 测试文件是否是目录。

1.3 重要方法

static boolean isHidden(Path path) 告知文件是否被 隐藏 static long size(Path path) 返回文件的大小(以字节为单位)。 static List readAllLines(Path path, Charset cs) 从文件中读取所有行。 static Path write(Path path, byte[] bytes, OpenOption… options) 将字节写入文件。 public static void main(String[] args) throws IOException { Path path = Paths.get("E:\\a.java"); Path path1 = Paths.get("E:\\b.java"); List<String> strings = Files.readAllLines(path); Files.write(path1, strings); }

二、测试

2.1 列出文件指定目录下的所有文件和子目录

说明:不包括递归的目录

static int count = 0; public static void main(String[] args) throws IOException { Path path2 = Paths.get("E:\\api"); Files.list(path2).forEach(path -> { count++; System.out.println(path); }); System.out.println(count); }

2.2 判断c盘的总空间,可用空间

public static void main(String[] args) throws IOException { Path path2 = Paths.get("E:\\api"); FileStore fileStore = Files.getFileStore(path2); System.out.println("C盘总共空间:" + fileStore.getTotalSpace()); System.out.println("C盘可用空间:" + fileStore.getUsableSpace()); }

三、Paths,Path类

3.1 简介

Paths类主要获取Path实例对象,Path类提供对路径的一系列操作

3.2 常用方法

public static Path get(String first, String… more)将路径字符串或从路径字符串连接起来的一系列字符串转换为Path boolean endsWith(Path other) 测试此路径是否以给定的路径结束。 Path getRoot() 返回此路径的根组分作为 Path对象,或 null如果该路径不具有根组件。 Path toAbsolutePath() 返回表示此路径的绝对路径的 Path对象。 URI toUri() 返回一个URI来表示此路径。

四、FileVisitor

4.1 简介

文件访问器

4.2 使用方法

Files.walkFileTree(path2,new SimpleFileVisitor (){…} 重写SimpleFileVisitor接口的方法,主要有: public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) 访问子目录前触发方法 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs); 访问文件时触发该方法 public FileVisitResult visitFileFailed(Path file, IOException exc); 访问文件失败时触发该方法。 public FileVisitResult postVisitDirectory(Path dir, IOException exc); 访问子目录后触发该方法

4.3 遍历输出指定目录下的所有文件夹和目录

增加全局变量就可以统计文件夹或者目录的个数

static int count = 0; public static void main(String[] args) throws IOException { Path path2 = Paths.get("E:\\com"); Files.walkFileTree(path2,new SimpleFileVisitor<Path>(){ @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { return super.preVisitDirectory(dir, attrs); } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { count ++; System.out.println(file); return super.visitFile(file, attrs); } @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { return super.visitFileFailed(file, exc); } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { count ++; System.out.println(dir); return super.postVisitDirectory(dir, exc); } }); System.out.println(count); }

结果:

E:\com\westos\ribbonconsumer\controller\HelloRibbonconsumer.java E:\com\westos\ribbonconsumer\controller E:\com\westos\ribbonconsumer\RibbonConsumerApplication.java E:\com\westos\ribbonconsumer E:\com\westos\springboothello\demo\controller\HelloController.java E:\com\westos\springboothello\demo\controller E:\com\westos\springboothello\demo\dao\StudentDAO.java E:\com\westos\springboothello\demo\dao E:\com\westos\springboothello\demo\DemoApplication.java E:\com\westos\springboothello\demo\domain\Student.java E:\com\westos\springboothello\demo\domain E:\com\westos\springboothello\demo E:\com\westos\springboothello\other\HellobController.java E:\com\westos\springboothello\other E:\com\westos\springboothello E:\com\westos E:\com 17

五、访问文件属性

5.1常用的接口

Interface FileAttribute 父接口

Interface AclFileAttributeView 支持读取或更新文件的访问控制列表(ACL)或文件所有者属性的文件属性视图。

Interface BasicFileAttributeView 文件属性视图,提供许多文件系统通用的基本文件属性集的视图

Interface DosFileAttributeView 提供传统“DOS”文件属性视图的文件属性视图。

Interface FileOwnerAttributeView 支持阅读或更新文件所有者的文件属性视图。

Interface PosixFileAttributeView 文件属性视图,提供通常与实现便携式操作系统接口(POSIX)系列标准的操作系统使用的文件系统上的文件关联的文件属性视图。

Interface UserDefinedFileAttributeView 文件属性视图,提供文件的用户定义属性(有时称为扩展属性)的视图 。

5.2 获取文件的创建时间

public static void main(String[] args) throws IOException { Path path2 = Paths.get("E:\\a.java"); BasicFileAttributeView basic = Files.getFileAttributeView(path2, BasicFileAttributeView.class); BasicFileAttributes basicFileAttributes = basic.readAttributes(); Date date = new Date(basicFileAttributes.creationTime().toMillis()); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(format.format(date).toString()); }

最新回复(0)