当前位置:网站首页>每天睡觉前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();
}
}
边栏推荐
- Matplotlib swordsman line - first acquaintance with Matplotlib
- During MySQL installation, mysqld Exe reports that the application cannot start normally (0xc000007b)`
- Chrome video download Plug-in – video downloader for Chrome
- 【Go实战基础】gin 如何自定义和使用一个中间件
- [go practical basis] how to install and use gin
- Knife4j 2. Solution to the problem of file control without selection when uploading x version files
- Microservice practice | Eureka registration center and cluster construction
- 【Go实战基础】gin 如何设置路由
- 「面试高频题」难度大 1.5/5,经典「前缀和 + 二分」运用题
- I've taken it. MySQL table 500W rows, but someone doesn't partition it?
猜你喜欢

Break the cocoon | one article explains what is the real cloud primordial

Probability is not yet. Look at statistical learning methods -- Chapter 4, naive Bayesian method

西瓜书--第五章.神经网络

Insight into cloud native | microservices and microservice architecture
![[go practical basis] how to verify request parameters in gin](/img/de/50db131d6993e5d955e3416c667c4c.png)
[go practical basis] how to verify request parameters in gin

Servlet全解:继承关系、生命周期、容器和请求转发与重定向等

微服务实战|熔断器Hystrix初体验

机器学习实战:《美人鱼》属于爱情片还是动作片?KNN揭晓答案

Solutions to Chinese garbled code in CMD window

How to use pyqt5 to make a sensitive word detection tool
随机推荐
Beats (filebeat, metricbeat), kibana, logstack tutorial of elastic stack
How to use pyqt5 to make a sensitive word detection tool
十年開發經驗的程序員告訴你,你還缺少哪些核心競爭力?
Chrome user script manager tempermonkey monkey
Chrome浏览器插件-Fatkun安装和介绍
What are the waiting methods of selenium
微服务实战|Eureka注册中心及集群搭建
自定義Redis連接池
Typeerror: X () got multiple values for argument 'y‘
Supplier selection and prequalification of Oracle project management system
The channel cannot be viewed when the queue manager is running
[go practical basis] how to bind and use URL parameters in gin
Win10 uses docker to pull the redis image and reports an error read only file system: unknown
机器学习之数据类型案例——基于朴素贝叶斯法,用数据辩男女
[go practical basis] how to set the route in gin
[go practical basis] how can gin get the request parameters of get and post
我服了,MySQL表500W行,居然有人不做分区?
In depth analysis of how the JVM executes Hello World
Demand delineation executive summary
概念到方法,绝了《统计学习方法》——第三章、k近邻法