当前位置:网站首页>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 !");
}
}
}
边栏推荐
- [go practical basis] how to verify request parameters in gin
- Difference between redis serialization genericjackson2jsonredisserializer and jackson2jsonredisserializer
- Don't look for it. All the necessary plug-ins for Chrome browser are here
- Redis sorted set data type API and application scenario analysis
- Pdf document of distributed service architecture: principle + Design + practice, (collect and see again)
- Double non undergraduate students enter the factory, while I am still quietly climbing trees at the bottom (Part 1)
- Matplotlib剑客行——布局指南与多图实现(更新)
- Chrome browser plug-in fatkun installation and introduction
- Hystrix implements request consolidation
- 每天睡觉前30分钟阅读_day4_Files
猜你喜欢

Ora-12514 problem solving method

告别996,IDEA中必装插件有哪些?

Don't look for it. All the necessary plug-ins for Chrome browser are here

Probability is not yet. Look at statistical learning methods -- Chapter 4, naive Bayesian method

企业级SaaS CRM实现

Actual combat of microservices | discovery and invocation of original ecosystem implementation services

JDBC回顾

Timed thread pool implements request merging

Chrome user script manager tempermonkey monkey

自定义Redis连接池
随机推荐
洞见云原生|微服务及微服务架构浅析
长篇总结(代码有注释)数构(C语言)——第四章、串(上)
Enterprise level SaaS CRM implementation
FragmentTabHost实现房贷计算器界面
每天睡觉前30分钟阅读_day3_Files
How to choose between efficiency and correctness of these three implementation methods of distributed locks?
Fragmenttabhost implements the interface of housing loan calculator
College Students' CET-4 and CET-6 composition template (self created version, successfully crossed CET-6)
每天睡前30分钟阅读Day6_Day6_Date_Calendar_LocalDate_TimeStamp_LocalTime
上班第一天的报错(Nessus安装winpcap报错)
Knife4j 2. Solution to the problem of file control without selection when uploading x version files
Microservice practice | declarative service invocation openfeign practice
Taking the upgrade of ByteDance internal data catalog architecture as an example, talk about the performance optimization of business system
Hystrix implements request consolidation
Creation and jump of activity
Ora-12514 problem solving method
破茧|一文说透什么是真正的云原生
[go practical basis] how to set the route in gin
概念到方法,绝了《统计学习方法》——第三章、k近邻法
Matplotlib剑客行——布局指南与多图实现(更新)