当前位置:网站首页>每天睡觉前30分钟阅读_day4_Files
每天睡觉前30分钟阅读_day4_Files
2022-07-02 06:34:00 【Janson666】
访问目录中的项
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();
}
}
边栏推荐
- ClassFile - Attributes - Code
- Cloud computing in my eyes - PAAS (platform as a service)
- [go practical basis] how to install and use gin
- 【Go实战基础】gin 如何设置路由
- 微服务实战|Eureka注册中心及集群搭建
- Matplotlib剑客行——初相识Matplotlib
- 聊聊消息队列高性能的秘密——零拷贝技术
- zk配置中心---Config Toolkit配置与使用
- Cloudreve自建云盘实践,我说了没人能限制得了我的容量和速度
- 一篇详解带你再次重现《统计学习方法》——第二章、感知机模型
猜你喜欢
How to use PHP spoole to implement millisecond scheduled tasks
概率还不会的快看过来《统计学习方法》——第四章、朴素贝叶斯法
Solution to amq4036 error in remote connection to IBM MQ
【Go实战基础】gin 如何验证请求参数
Programmers with ten years of development experience tell you, what core competitiveness do you lack?
Matplotlib swordsman line - first acquaintance with Matplotlib
自定義Redis連接池
[go practical basis] how to verify request parameters in gin
Matplotlib swordsman - a stylist who can draw without tools and code
【Go实战基础】gin 如何绑定与使用 url 参数
随机推荐
【Go实战基础】gin 如何自定义和使用一个中间件
How to use pyqt5 to make a sensitive word detection tool
Ora-12514 problem solving method
远程连接IBM MQ报错AMQ4036解决方法
Taking the upgrade of ByteDance internal data catalog architecture as an example, talk about the performance optimization of business system
Pool de connexion redis personnalisé
自定義Redis連接池
Double non undergraduate students enter the factory, while I am still quietly climbing trees at the bottom (Part 1)
Idea view bytecode configuration
Customize redis connection pool
WSL installation, beautification, network agent and remote development
一篇详解带你再次重现《统计学习方法》——第二章、感知机模型
Mysql 多列IN操作
使用IBM MQ远程连接时报错AMQ 4043解决思路
Matplotlib剑客行——容纳百川的艺术家教程
京东高级工程师开发十年,编写出:“亿级流量网站架构核心技术”
oracle修改数据库字符集
Microservice practice | declarative service invocation openfeign practice
Hystrix implements request consolidation
[staff] common symbols of staff (Hualian clef | treble clef | bass clef | rest | bar line)