当前位置:网站首页>Read 30 minutes before going to bed every day_ day3_ Files
Read 30 minutes before going to bed every day_ day3_ Files
2022-07-02 09:33:00 【Janson666】
One 、 Operation file :
Path and Files Class encapsulates all the functions needed to process the file system on the user's machine .
1.Path class
Method :
- 1.// Get file path
- Paths.get(“ route ”)
- Paths.get(“/home”,“my”)
/home/my
By the above methods , After getting the file path , It can be done by Path Class object , Call some methods below ,
path.getFileName()path.getParent()path.getRoot()path.getFileSystem()
2. Files class
Method :
- Method :
Files.readAllBytes(path) // Read all the contents of the fileFiles.readAllLines(path) // Read line by lineFiles.exists(path) // Determine whether the directory or file existsFiles.createDirectory(path)Files.createFile(path)Files.deleteIfExists(path)Files.delete() // Delete directly without judgment
3. Code test of the above method :
/** * @Author Janson * @Date 2022/4/18 10:12 * @Version 1.0 */
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
/** * Paths class * Method : * Paths.get(" route ") * Paths.get("/home","my") * /home/my * path.getFileName() * path.getParent() * path.getRoot() * path.getFileSystem() * Files class * Method : * Files.readAllBytes(path) * Files.readAllLines(path) * Files.exists(path) * Files.createDirectory(path) * Files.createFile(path) * Files.deleteIfExists(path) * Files.delete() // Delete directly without judgment * */
public class NioPrac {
public static void main(String[] args) throws IOException {
// Get the absolute path of the current user ( The path above the relative path )
String userDir = System.getProperty("user.dir");
System.out.println(userDir);
Path path = Paths.get(userDir, "testData/longWord");
System.out.println(path);
//Paths Method tests in classes
System.out.println("1 " + path.getFileName());
System.out.println("2 " + path.getParent());
System.out.println("3 " + path.getRoot());
System.out.println("4 " + path.getFileSystem());
List<String> strings = Files.readAllLines(path);
strings.forEach(string-> System.out.println("stringArrays======>" + string));
String stringArray = new String(Files.readAllBytes(path));
//for (s stringByte : stringArrays) {
// System.out.println("stringByte ======> " + stringByte);
//}
System.out.println("stringArray--------=====>" + stringArray);
// Judge before creating the directory , Whether the directory exists
boolean exists = Files.exists(Paths.get("O:\\code\\BeforeSleeping30m\\testData\\my2"));
if (!exists) {
Path directory = Files.createDirectory(Paths.get("O:\\code\\BeforeSleeping30m\\testData\\my2"));
System.out.println("directory====>"+ directory);
}
// create a file , Determine whether there is a file before creating , If exist , Do not create
boolean existsFile = Files.exists(Paths.get("O:\\code\\BeforeSleeping30m\\testData\\my\\my2.txt"));
if (!existsFile) {
Path file = Files.createFile(Paths.get("O:\\code\\BeforeSleeping30m\\testData\\my\\my2.txt"));
System.out.println(" The newly created file The path of :" + file);
}
boolean deleteIfExists = Files.deleteIfExists(Paths.get("O:\\code\\BeforeSleeping30m\\testData\\my\\my2.txt"));
if (deleteIfExists){
System.out.println(" Delete successful !");
}
}
}
边栏推荐
- What are the differences between TP5 and laravel
- 微服务实战|微服务网关Zuul入门与实战
- 数构(C语言--代码有注释)——第二章、线性表(更新版)
- [go practical basis] how to customize and use a middleware in gin
- 微服务实战|Eureka注册中心及集群搭建
- Chrome浏览器插件-Fatkun安装和介绍
- 数构(C语言)——第四章、矩阵的压缩存储(下)
- C语言之做木桶
- BugkuCTF-web24(解题思路及步骤)
- How to choose between efficiency and correctness of these three implementation methods of distributed locks?
猜你喜欢

数构(C语言--代码有注释)——第二章、线性表(更新版)

Chrome browser tag management plug-in – onetab

Ora-12514 problem solving method

Oracle modify database character set

Solution to amq4036 error in remote connection to IBM MQ

DTM distributed transaction manager PHP collaboration client V0.1 beta release!!!

Chrome视频下载插件–Video Downloader for Chrome

Matplotlib剑客行——布局指南与多图实现(更新)
![[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

十年開發經驗的程序員告訴你,你還缺少哪些核心競爭力?
随机推荐
Micro service practice | introduction and practice of zuul, a micro service gateway
十年開發經驗的程序員告訴你,你還缺少哪些核心競爭力?
Mysql默认事务隔离级别及行锁
MySql报错:unblock with mysqladmin flush-hosts
Matplotlib swordsman - a stylist who can draw without tools and code
分布式锁的这三种实现方式,如何在效率和正确性之间选择?
In depth analysis of how the JVM executes Hello World
ClassFile - Attributes - Code
C语言之判断直角三角形
Chrome浏览器插件-Fatkun安装和介绍
Alibaba /热门json解析开源项目 fastjson2
Taking the upgrade of ByteDance internal data catalog architecture as an example, talk about the performance optimization of business system
Chrome browser plug-in fatkun installation and introduction
Thinkphp5 how to determine whether a table exists
MySQL multi column in operation
Don't look for it. All the necessary plug-ins for Chrome browser are here
西瓜书--第五章.神经网络
大学生四六级作文模板(自创版,成功跨过六级)
From concept to method, the statistical learning method -- Chapter 3, k-nearest neighbor method
The channel cannot be viewed when the queue manager is running