NIO工具类Files

/*
 BIO: 同步阻塞IO
   NIO: 同步非阻塞IO。 NIO是基于通道,缓冲区,和选择器....
   AIO: 异步非阻塞IO。
*/
    jdk7之后,提供了一个NIO的工具类,叫做Files

    相关API:
        Path: 表示一个路径。
        Paths: 路径的工具类,可以获取一个Path对象
        Files: 操作IO的工具类

    Paths中有一个方法,可以获取到一个Path对象
        static Path get​(String first, String... more): 根据多个路径片段组合成一个path对象

    Files中的方法:
        static Path copy​(Path source, Path target, CopyOption... options) :复制
        参数source:源文件
        参数target:目标文件
        参数options: 以什么方式复制。 可以传递REPLACE_EXISTING。 表示如果文件存在就替换

        static List<String> readAllLines​(Path path):读取文件中的所有的行,并且保存在集合中
 */
public class Demo02Files {
    public static void main(String[] args) throws IOException {
        method2();
    }
    // static List<String> readAllLines​(Path path):读取文件中的所有的行,并且保存在集合中
    public static void method2() throws IOException {
        List<String> list = Files.readAllLines(Paths.get("outTeacherTable.txt"));
        for (String s : list) {
            System.out.println(s);
        }
    }

    //对文件的复制
    public static void method1() throws IOException {
        Path source = Paths.get("d:", "aa.jpg");
        Path dest = Paths.get("e:", "bb.jpg");
        //调用Files的copy进行复制
        Files.copy(source, dest, StandardCopyOption.REPLACE_EXISTING);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值