当前位置:网站首页>Read 30 minutes before going to bed every day_ day4_ Files
Read 30 minutes before going to bed every day_ day4_ Files
2022-07-02 09:33:00 【Janson666】
Access items in the directory
1. Get all directories and files in the current path ( Get subdirectories non recursively )list Method
Stream<Path> pathLists = Files.list(path);

2. Get all directories and files in the current path ( Get subdirectories recursively )walk Method
Stream<Path> walkPaths = Files.walk(path);

3. Get all directories and files in the current path ( Get subdirectories recursively ( The specified depth is 2))walk Method
Stream<Path> walkPathsDepth = Files.walk(path, 2);

4. Get the properties of the file readAttributes() Method
**{@code “*”} then all attributes are read.** adopt Pass in the parameter * You can get all attributes
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"));
The result is map The saved 
5.FileChannel Call static methods open Create channels , Parameters (path,StandardOpenOption)
Path pa = Paths.get("O:\\code\\BeforeSleeping30m\\testData\\my\\my1.txt"); FileChannel channel = FileChannel.open(pa, StandardOpenOption.APPEND);
StandardOpenOption Yes WRITE,APPEND,TRUNCATE_EXUSTING( If a file exists when writing , Delete the original ),CREATE
6.Buffer class (ByteBuffer,CharBuffer) etc.
get: obtain buffer Bytes in
put: towards buffer Push bytes in
clear() : The pointer is reset to 0, The limit is set to capacity , Prepare for writing
flip: Read write conversion , After reading , You need to get the content , Before getting the method, you need to call , Return the pointer to 0;
rewind: The read / write position pointer returns 0, Prepare for rewriting .
remaining: Returns the number of remaining values read in or written out ;
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);
// Read write conversion
buffer.flip();
// Set the pointer position to 0
buffer.rewind();
long position = channel.position();
System.out.println(" channel.size() "+ channel.size());
System.out.println("position:"+ position);
// Position reset to 0, The limit is set to capacity
buffer.clear();
channel.close();
System.out.println();
All the codes of this test :
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 Method does not enter subdirectories . As can be seen from the results
Stream<Path> pathLists = Files.list(path);
pathLists.forEach(pathList->System.out.println("testData All directories under ( The subdirectory path of the directory will not be read ):" + pathList));
//walk Method Society Read all directory paths under the directory , Include subdirectories
System.out.println("================ Split line ==================");
Stream<Path> walkPaths = Files.walk(path);
walkPaths.forEach(walkPath->System.out.println("test All directories under ( The path of subdirectories in the directory will also be read :)"+ walkPath));
System.out.println("================ Split line ==================");
// Specify the depth of traversing subdirectories
Stream<Path> walkPathsDepth = Files.walk(path, 2);
walkPathsDepth.forEach(walkPathDepth -> System.out.println(" The specified depth is 2 layer :" + walkPathDepth));
// Get the properties of the file
//{@code "*"} then all attributes are read.
// LinkOption.NOFOLLOW_LINKS Don't follow the symbolic connection
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);
// Read write conversion
buffer.flip();
// Set the pointer position to 0
buffer.rewind();
long position = channel.position();
System.out.println(" channel.size() "+ channel.size());
System.out.println("position:"+ position);
// Position reset to 0, The limit is set to capacity
buffer.clear();
channel.close();
System.out.println();
}
}
边栏推荐
- In depth analysis of how the JVM executes Hello World
- Ora-12514 problem solving method
- Mysql 多列IN操作
- 一篇详解带你再次重现《统计学习方法》——第二章、感知机模型
- Redis 序列化 GenericJackson2JsonRedisSerializer和Jackson2JsonRedisSerializer的区别
- Typeerror: X () got multiple values for argument 'y‘
- C语言之二进制与十进制
- C语言之数据插入
- What are the waiting methods of selenium
- Matplotlib剑客行——初相识Matplotlib
猜你喜欢

I've taken it. MySQL table 500W rows, but someone doesn't partition it?

Typora安装包分享

Enterprise level SaaS CRM implementation

微服务实战|原生态实现服务的发现与调用

Timed thread pool implements request merging

知识点很细(代码有注释)数构(C语言)——第三章、栈和队列

自定义Redis连接池

机器学习实战:《美人鱼》属于爱情片还是动作片?KNN揭晓答案
![[go practical basis] how to customize and use a middleware in gin](/img/fb/c0a4453b5d3fda845c207c0cb928ae.png)
[go practical basis] how to customize and use a middleware in gin

Watermelon book -- Chapter 6 Support vector machine (SVM)
随机推荐
DTM distributed transaction manager PHP collaboration client V0.1 beta release!!!
Matplotlib剑客行——布局指南与多图实现(更新)
Ora-12514 problem solving method
Complete solution of servlet: inheritance relationship, life cycle, container, request forwarding and redirection, etc
Knife4j 2. Solution to the problem of file control without selection when uploading x version files
CKEditor 4.10.1 上传图片提示“不正确的服务器响应” 问题解决
Programmers with ten years of development experience tell you, what core competitiveness do you lack?
[go practical basis] gin efficient artifact, how to bind parameters to structures
Bold prediction: it will become the core player of 5g
微服务实战|Eureka注册中心及集群搭建
I've taken it. MySQL table 500W rows, but someone doesn't partition it?
Microservice practice | teach you to develop load balancing components hand in hand
个人经历&&博客现状
分布式锁的这三种实现方式,如何在效率和正确性之间选择?
洞见云原生|微服务及微服务架构浅析
西瓜书--第六章.支持向量机(SVM)
How to use pyqt5 to make a sensitive word detection tool
上班第一天的报错(AWVS卸载不彻底)
Mathematics in machine learning -- point estimation (I): basic knowledge
Attributes of classfile