当前位置:网站首页>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 !");
}
}
}
边栏推荐
- Double non undergraduate students enter the factory, while I am still quietly climbing trees at the bottom (Part 1)
- Machine learning practice: is Mermaid a love movie or an action movie? KNN announces the answer
- Chrome user script manager tempermonkey monkey
- Chrome browser tag management plug-in – onetab
- Chrome video download Plug-in – video downloader for Chrome
- Learn combinelatest through a practical example
- [go practical basis] how to set the route in gin
- JVM instruction mnemonic
- Chrome browser plug-in fatkun installation and introduction
- Timed thread pool implements request merging
猜你喜欢

Number structure (C language) -- Chapter 4, compressed storage of matrices (Part 2)

Mysql默认事务隔离级别及行锁

How to use PHP spoole to implement millisecond scheduled tasks

Microservice practice | load balancing component and source code analysis

Operation and application of stack and queue

《统计学习方法》——第五章、决策树模型与学习(上)

微服务实战|负载均衡组件及源码分析

在SQL注入中,为什么union联合查询,id必须等于0

Redis installation and deployment (windows/linux)

Chrome video download Plug-in – video downloader for Chrome
随机推荐
Mysql默认事务隔离级别及行锁
Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedd
Break the cocoon | one article explains what is the real cloud primordial
Actual combat of microservices | discovery and invocation of original ecosystem implementation services
Redis installation and deployment (windows/linux)
Matplotlib swordsman line - first acquaintance with Matplotlib
个人经历&&博客现状
Dix ans d'expérience dans le développement de programmeurs vous disent quelles compétences de base vous manquez encore?
AMQ 4043 solution for errors when using IBM MQ remote connection
Oracle modifies tablespace names and data files
BugkuCTF-web24(解题思路及步骤)
Microservice practice | fuse hytrix initial experience
Creation and jump of activity
MySql报错:unblock with mysqladmin flush-hosts
Ora-12514 problem solving method
Chrome user script manager tempermonkey monkey
Safety production early warning system software - Download safety production app software
Say goodbye to 996. What are the necessary plug-ins in idea?
"Interview high frequency question" is 1.5/5 difficult, and the classic "prefix and + dichotomy" application question
Watermelon book -- Chapter 6 Support vector machine (SVM)