每天睡觉前30分钟阅读_day4_Files

本文详细介绍了Java NIO中的文件操作,包括使用list()、walk()方法获取目录内容,利用readAttributes()读取文件属性,以及FileChannel的使用,如打开、追加、读写转换等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

访问目录中的项

1.获取当前路径的所有目录和文件(非递归获取子目录)list 方法
  •  Stream<Path> pathLists = Files.list(path);
    

在这里插入图片描述

2.获取当前路径的所有目录和文件(递归获取子目录)walk 方法
  • Stream<Path> walkPaths = Files.walk(path);
    

在这里插入图片描述

3.获取当前路径的所有目录和文件(递归获取子目录(指定深度为 2))walk 方法
  • Stream<Path> walkPathsDepth = Files.walk(path, 2);
    

在这里插入图片描述

4.获取文件的属性 readAttributes() 方法

**{@code “*”} then all attributes are read.**通过 传入参数 * 可获得全部属性

  • Map<String, Object> attrMap = Files.readAttributes(path, "*", LinkOption.NOFOLLOW_LINKS);
    
  • Set<String> keySet = attrMap.keySet();
    
  • keySet.forEach(key -> System.out.println(key));
    
  • System.out.println(attrMap.get("lastAccessTime"));
    

结果是以map保存的
在这里插入图片描述

5.FileChannel 调用惊静态方法 open创建通道,参数(path,StandardOpenOption)
  • Path pa = Paths.get("O:\\code\\BeforeSleeping30m\\testData\\my\\my1.txt");
    FileChannel channel = FileChannel.open(pa, StandardOpenOption.APPEND);
    

StandardOpenOption有 WRITE,APPEND,TRUNCATE_EXUSTING(在写入时如果存在文件,则删除原有的),CREATE

6.Buffer 类(ByteBuffer,CharBuffer)等
  • get:获取buffer中的字节

  • put:向buffer中推入字节

  • clear() : 指针复位为0,界限设置为容量,为写出做准备

  • flip:读写转换,在读完后,需要获取其中内容,在获取方法前需要调用,将指针归0;

  • rewind:读写位置指针归0,为重新写入做准备。

  • remaining:返回剩余读入或写出的值的数量;

        Path pa = Paths.get("O:\\code\\BeforeSleeping30m\\testData\\my\\my1.txt");
        FileChannel channel = FileChannel.open(pa, StandardOpenOption.APPEND);
        String s = "I am  a man";
        ByteBuffer buffer = ByteBuffer.wrap(s.getBytes());
        int i = channel.write(buffer);
        //读写转换
        buffer.flip();
        //将指针位置归0
        buffer.rewind();
        long position = channel.position();
        System.out.println(" channel.size() "+ channel.size());
        System.out.println("position:"+ position);
        //位置复位到0,界限设置到容量
        buffer.clear();
        channel.close();
        System.out.println();

本次测试的全部代码:

import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.*;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

/**
 * @Author Janson
 * @Date 2022/4/20 9:17
 * @Version 1.0
 */
public class Day4_Files {
    public static void main(String[] args) throws Exception  {
        Path path = Paths.get("O:\\code\\BeforeSleeping30m\\testData");

        //list方法不会进入子目录。从结果中可以看出
        Stream<Path> pathLists = Files.list(path);
        pathLists.forEach(pathList->System.out.println("testData下的所有目录(不会读取目录的子目录路径):" + pathList));

        //walk方法会 读取目录下所有的目录路径,包含子目录
        System.out.println("================分割线==================");
        Stream<Path> walkPaths = Files.walk(path);
        walkPaths.forEach(walkPath->System.out.println("test下的所有目录(会将目录中子目录的路径也读取:)"+ walkPath));

        System.out.println("================分割线==================");
        //指定遍历子目录的深度
        Stream<Path> walkPathsDepth = Files.walk(path, 2);
        walkPathsDepth.forEach(walkPathDepth -> System.out.println("指定深度为2层:" + walkPathDepth));

        //获取文件的属性
        //{@code "*"} then all attributes are read.
        // LinkOption.NOFOLLOW_LINKS 不要跟随符号连接
        Map<String, Object> attrMap = Files.readAttributes(path, "*", LinkOption.NOFOLLOW_LINKS);
        Set<String> keySet = attrMap.keySet();
        keySet.forEach(key -> System.out.println(key));
        System.out.println(attrMap.get("lastAccessTime"));

        Path pa = Paths.get("O:\\code\\BeforeSleeping30m\\testData\\my\\my1.txt");
        FileChannel channel = FileChannel.open(pa, StandardOpenOption.APPEND);
        String s = "I am  a man";
        ByteBuffer buffer = ByteBuffer.wrap(s.getBytes());
        int i = channel.write(buffer);
        //读写转换
        buffer.flip();
        //将指针位置归0
        buffer.rewind();
        long position = channel.position();
        System.out.println(" channel.size() "+ channel.size());
        System.out.println("position:"+ position);
        //位置复位到0,界限设置到容量
        buffer.clear();
        channel.close();
        System.out.println();

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Janson666

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

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

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

打赏作者

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

抵扣说明:

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

余额充值