当前位置:网站首页>每天睡觉前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();
}
}
边栏推荐
- idea查看字节码配置
- Pool de connexion redis personnalisé
- Oracle modifies tablespace names and data files
- [go practical basis] how to bind and use URL parameters in gin
- How to use PHP spoole to implement millisecond scheduled tasks
- MySQL error: unblock with mysqladmin flush hosts
- Discussion on improving development quality and reducing test bug rate
- 洞见云原生|微服务及微服务架构浅析
- Solution to amq4036 error in remote connection to IBM MQ
- MySql报错:unblock with mysqladmin flush-hosts
猜你喜欢
互联网API接口幂等设计
Machine learning practice: is Mermaid a love movie or an action movie? KNN announces the answer
Probability is not yet. Look at statistical learning methods -- Chapter 4, naive Bayesian method
Data type case of machine learning -- using data to distinguish men and women based on Naive Bayesian method
Programmers with ten years of development experience tell you, what core competitiveness do you lack?
以字节跳动内部 Data Catalog 架构升级为例聊业务系统的性能优化
远程连接IBM MQ报错AMQ4036解决方法
Typeerror: X () got multiple values for argument 'y‘
自定義Redis連接池
oracle修改数据库字符集
随机推荐
Beats (filebeat, metricbeat), kibana, logstack tutorial of elastic stack
Oracle modifies tablespace names and data files
Microservice practice | fuse hytrix initial experience
长篇总结(代码有注释)数构(C语言)——第四章、串(上)
洞见云原生|微服务及微服务架构浅析
Dix ans d'expérience dans le développement de programmeurs vous disent quelles compétences de base vous manquez encore?
我服了,MySQL表500W行,居然有人不做分区?
Matplotlib swordsman Tour - an artist tutorial to accommodate all rivers
Flink-使用流批一体API统计单词数量
Redis sorted set data type API and application scenario analysis
Machine learning practice: is Mermaid a love movie or an action movie? KNN announces the answer
Thinkphp5 how to determine whether a table exists
How to use pyqt5 to make a sensitive word detection tool
Knife4j 2.X版本文件上传无选择文件控件问题解决
Matplotlib剑客行——布局指南与多图实现(更新)
数构(C语言)——第四章、矩阵的压缩存储(下)
Redis 序列化 GenericJackson2JsonRedisSerializer和Jackson2JsonRedisSerializer的区别
微服务实战|Eureka注册中心及集群搭建
【Go实战基础】gin 如何设置路由
以字节跳动内部 Data Catalog 架构升级为例聊业务系统的性能优化