访问目录中的项
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();
}
}