当前位置:网站首页>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();
}
}
边栏推荐
- FragmentTabHost实现房贷计算器界面
- Chrome video download Plug-in – video downloader for Chrome
- Ora-12514 problem solving method
- Programmers with ten years of development experience tell you, what core competitiveness do you lack?
- 双非本科生进大厂,而我还在底层默默地爬树(上)
- Pdf document of distributed service architecture: principle + Design + practice, (collect and see again)
- Idea view bytecode configuration
- VIM operation command Encyclopedia
- Difference between redis serialization genericjackson2jsonredisserializer and jackson2jsonredisserializer
- 每天睡前30分钟阅读Day5_Map中全部Key值,全部Value值获取方式
猜你喜欢
记录一下初次使用Xray的有趣过程
Probability is not yet. Look at statistical learning methods -- Chapter 4, naive Bayesian method
How to install PHP in CentOS
Hystrix implements request consolidation
BugkuCTF-web24(解题思路及步骤)
Actual combat of microservices | discovery and invocation of original ecosystem implementation services
微服务实战|手把手教你开发负载均衡组件
Chrome视频下载插件–Video Downloader for Chrome
Don't look for it. All the necessary plug-ins for Chrome browser are here
Chrome browser plug-in fatkun installation and introduction
随机推荐
大学生四六级作文模板(自创版,成功跨过六级)
Watermelon book -- Chapter 5 neural network
Idea view bytecode configuration
微服务实战|微服务网关Zuul入门与实战
分布式锁的这三种实现方式,如何在效率和正确性之间选择?
JVM instruction mnemonic
Matplotlib swordsman line - first acquaintance with Matplotlib
互联网API接口幂等设计
Redis installation and deployment (windows/linux)
I've taken it. MySQL table 500W rows, but someone doesn't partition it?
Matplotlib剑客行——没有工具用代码也能画图的造型师
Number structure (C language -- code with comments) -- Chapter 2, linear table (updated version)
VIM operation command Encyclopedia
A detailed explanation takes you to reproduce the statistical learning method again -- Chapter 2, perceptron model
Difference between redis serialization genericjackson2jsonredisserializer and jackson2jsonredisserializer
Troubleshooting and handling of an online problem caused by redis zadd
Typeerror: X () got multiple values for argument 'y‘
VIM操作命令大全
FragmentTabHost实现房贷计算器界面
BugkuCTF-web24(解题思路及步骤)