当前位置:网站首页>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();
}
}
边栏推荐
- Timed thread pool implements request merging
- 每天睡觉前30分钟阅读_day3_Files
- web安全与防御
- Insight into cloud native | microservices and microservice architecture
- [go practical basis] how to set the route in gin
- 西瓜书--第五章.神经网络
- 长篇总结(代码有注释)数构(C语言)——第四章、串(上)
- 微服务实战|负载均衡组件及源码分析
- Redis 序列化 GenericJackson2JsonRedisSerializer和Jackson2JsonRedisSerializer的区别
- 因上努力,果上随缘
猜你喜欢

互联网API接口幂等设计

Microservice practice | declarative service invocation openfeign practice

Timed thread pool implements request merging

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

Solution to amq4036 error in remote connection to IBM MQ

Troubleshooting and handling of an online problem caused by redis zadd

JDBC回顾

Number structure (C language -- code with comments) -- Chapter 2, linear table (updated version)

Creation and jump of activity

Chrome browser plug-in fatkun installation and introduction
随机推荐
概率还不会的快看过来《统计学习方法》——第四章、朴素贝叶斯法
Operation and application of stack and queue
Knowledge points are very detailed (code is annotated) number structure (C language) -- Chapter 3, stack and queue
C语言之做木桶
Chrome browser tag management plug-in – onetab
数构(C语言--代码有注释)——第二章、线性表(更新版)
Oracle delete tablespace and user
How to choose between efficiency and correctness of these three implementation methods of distributed locks?
Elastic Stack之Beats(Filebeat、Metricbeat)、Kibana、Logstash教程
Programmers with ten years of development experience tell you, what core competitiveness do you lack?
双非本科生进大厂,而我还在底层默默地爬树(上)
Number structure (C language) -- Chapter 4, compressed storage of matrices (Part 2)
概念到方法,绝了《统计学习方法》——第三章、k近邻法
Actual combat of microservices | discovery and invocation of original ecosystem implementation services
攻防世界-Web进阶区-unserialize3
Matplotlib swordsman line - first acquaintance with Matplotlib
Chrome浏览器插件-Fatkun安装和介绍
What are the differences between TP5 and laravel
BugkuCTF-web24(解题思路及步骤)
Don't look for it. All the necessary plug-ins for Chrome browser are here