当前位置:网站首页>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();
}
}
边栏推荐
- Difference between redis serialization genericjackson2jsonredisserializer and jackson2jsonredisserializer
- "Redis source code series" learning and thinking about source code reading
- JDBC回顾
- [go practical basis] how can gin get the request parameters of get and post
- Fragmenttabhost implements the interface of housing loan calculator
- In depth analysis of how the JVM executes Hello World
- Redis installation and deployment (windows/linux)
- How to use PHP spoole to implement millisecond scheduled tasks
- "Interview high frequency question" is 1.5/5 difficult, and the classic "prefix and + dichotomy" application question
- 长篇总结(代码有注释)数构(C语言)——第四章、串(上)
猜你喜欢
Operation and application of stack and queue
微服务实战|手把手教你开发负载均衡组件
Matplotlib swordsman line - first acquaintance with Matplotlib
web安全与防御
Probability is not yet. Look at statistical learning methods -- Chapter 4, naive Bayesian method
Chrome browser plug-in fatkun installation and introduction
Creation and jump of activity
Solution to amq4036 error in remote connection to IBM MQ
微服务实战|熔断器Hystrix初体验
微服务实战|原生态实现服务的发现与调用
随机推荐
Redis installation and deployment (windows/linux)
Microservice practice | fuse hytrix initial experience
数构(C语言)——第四章、矩阵的压缩存储(下)
Talk about the secret of high performance of message queue -- zero copy technology
Timed thread pool implements request merging
Fragmenttabhost implements the interface of housing loan calculator
MySql报错:unblock with mysqladmin flush-hosts
Number structure (C language -- code with comments) -- Chapter 2, linear table (updated version)
Taking the upgrade of ByteDance internal data catalog architecture as an example, talk about the performance optimization of business system
因上努力,果上随缘
微服务实战|声明式服务调用OpenFeign实践
Data type case of machine learning -- using data to distinguish men and women based on Naive Bayesian method
Cloudrev self built cloud disk practice, I said that no one can limit my capacity and speed
Typora安装包分享
Break the cocoon | one article explains what is the real cloud primordial
Troubleshooting and handling of an online problem caused by redis zadd
web安全与防御
How to use pyqt5 to make a sensitive word detection tool
From concept to method, the statistical learning method -- Chapter 3, k-nearest neighbor method
[go practical basis] gin efficient artifact, how to bind parameters to structures