Java NIO学习(一) Path、Paths 和 Files工具类的使用

JDK1.7 引入了新的IO操作类。在java.nio.file包下,包括Files、Paths等工具类。

中文文档:http://www.matools.com/file/manual/jdk_api_1.8_google/java/nio/file/package-summary.html

1.创建文件或目录

/**
 * 创建文件或目录
 */
private static void createFileOrDir() {
    try {
        // 创建新目录,除了最后一个部件,其他必须是存在的
        Files.createDirectory(Paths.get("F:/test"));

        // 创建路径中的中间目录,能创建不存在的中间部件
        Files.createDirectories(Paths.get("F:/test/test"));

        // 创建文件及相关目录
        Path path = Paths.get("F:/test/testbak.txt");
        Files.createDirectories(path.getParent());
        Files.createFile(path);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

2.删除文件

/**
 * 删除文件
 */
private static void deleteFile() {
    Path p = Paths.get("F:/test.txt");
    try {
        Files.delete(p);// 用static boolean deleteIfExists(Path path)方法比较好
        System.out.println("删除成功");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

3.移动文件

/**
 * 移动文件
 */
private static void moveFile() {
    Path pSrc = Paths.get("F:/test.txt");
    Path pDest = Paths.get("E:/test.txt");
    try {
        Files.move(pSrc, pDest, StandardCopyOption.REPLACE_EXISTING);
        System.out.println("移动成功");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

4.复制文件

/**
 * 复制文件
 */
private static void copyFile() {
    Path pSrc = Paths.get("F:/test.txt");
    Path pDest = Paths.get("F:/testbak.txt");
    try {
        Files.copy(pSrc, pDest, StandardCopyOption.REPLACE_EXISTING);
        System.out.println("复制成功");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

5.从文件读取数据

/**
 * 从文件读取数据
 */
private static void readFromFile() {
    Path p = Paths.get("F:/", "test.txt");
    try {
        byte[] bytes = Files.readAllBytes(p);
        System.out.println(new String(bytes));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

6.从文件按行读取数据

/**
 * 从文件按行读取数据
 */
private static void readByLineFromFile() {
    Path path = Paths.get("F:/", "test.txt");
    try {
    	// 方式1
        List<String> lines = Files.lines(path).collect(Collectors.toList());
        System.out.println(lines);
        
        // 方式2
        BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);
        String str = null;
        while((str = reader.readLine()) != null){
            System.out.println(str);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

如果方式2指定的字符编码不对,可能会抛出异常 MalformedInputException ,或者读取到了乱码:

7.向文件写入数据

/**
 * 向文件写入数据
 */
private static void write2File() {
    // 获得路径
    Path path = Paths.get("F:/", "test.txt");
    String info = "I love java really,你喜欢什么?";
    try {
        // 方式1
        Files.write(path, info.getBytes("utf8"), StandardOpenOption.APPEND);

		// 方式2
        BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8);
        writer.write(info );
        writer.flush();
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

8.获得文件路径的几种方法

/**
 * 获得文件路径的几种方法
 */
private static void getFilePath() {
    File file = new File("F:/test.txt");
    // Path
    Path p1 = Paths.get("F:/", "test.txt");// F:\test.txt
    System.out.println(p1);

    Path p2 = file.toPath();
    System.out.println(p2);

    Path p3 = FileSystems.getDefault().getPath("F:/", "test.txt");
    System.out.println(p3);
}






参考地址:https://www.cnblogs.com/maxigang/p/9044192.html
参考地址:https://www.cnblogs.com/zxfei/p/10901364.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不愿放下技术的小赵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值