当前位置:网站首页>每天睡觉前30分钟阅读_day3_Files
每天睡觉前30分钟阅读_day3_Files
2022-07-02 06:34:00 【Janson666】
一、操作文件:
Path和Files类封装了用户机器上处理文件系统所需要的所有功能。
1.Path类
方法:
- 1.//获取文件路径
- Paths.get(“路径”)
- Paths.get(“/home”,“my”)
/home/my
通过上述方法,获取文件路径后,可通过Path类的对象,调用下边的一些方法,
path.getFileName()path.getParent()path.getRoot()path.getFileSystem()
2. Files 类
方法:
- 方法:
Files.readAllBytes(path) //全部读入文件内容Files.readAllLines(path) //一行一行读取Files.exists(path) //判断目录或者文件是否存在Files.createDirectory(path)Files.createFile(path)Files.deleteIfExists(path)Files.delete() //没有判断直接删除
3.上述方法的代码测试:
/** * @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 类 * 方法: * Paths.get("路径") * Paths.get("/home","my") * /home/my * path.getFileName() * path.getParent() * path.getRoot() * path.getFileSystem() * Files 类 * 方法: * Files.readAllBytes(path) * Files.readAllLines(path) * Files.exists(path) * Files.createDirectory(path) * Files.createFile(path) * Files.deleteIfExists(path) * Files.delete() //没有判断直接删除 * */
public class NioPrac {
public static void main(String[] args) throws IOException {
//获取当前用户的绝对路径(相对路径以上的路径)
String userDir = System.getProperty("user.dir");
System.out.println(userDir);
Path path = Paths.get(userDir, "testData/longWord");
System.out.println(path);
//Paths 类中的方法测试
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);
//在创建目录前进行判断,是否存在该目录
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);
}
//创建文件,创建前先判断是否存在文件,若存在,则不进行创建
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("新创建的file的路径:" + file);
}
boolean deleteIfExists = Files.deleteIfExists(Paths.get("O:\\code\\BeforeSleeping30m\\testData\\my\\my2.txt"));
if (deleteIfExists){
System.out.println("删除成功!");
}
}
}
边栏推荐
- Matplotlib swordsman - a stylist who can draw without tools and code
- 定时线程池实现请求合并
- Dix ans d'expérience dans le développement de programmeurs vous disent quelles compétences de base vous manquez encore?
- 聊聊消息队列高性能的秘密——零拷贝技术
- [staff] time mark and note duration (staff time mark | full note rest | half note rest | quarter note rest | eighth note rest | sixteenth note rest | thirty second note rest)
- A detailed explanation takes you to reproduce the statistical learning method again -- Chapter 2, perceptron model
- Learn combinelatest through a practical example
- Solutions to Chinese garbled code in CMD window
- 别找了,Chrome浏览器必装插件都在这了
- Cloud computing in my eyes - PAAS (platform as a service)
猜你喜欢

Microservice practice | Eureka registration center and cluster construction

I've taken it. MySQL table 500W rows, but someone doesn't partition it?

idea查看字节码配置
![[go practical basis] how to bind and use URL parameters in gin](/img/63/84717b0da3a55d7fda9d57c8da2463.png)
[go practical basis] how to bind and use URL parameters in gin

盘点典型错误之TypeError: X() got multiple values for argument ‘Y‘

Solution to amq4036 error in remote connection to IBM MQ

Matplotlib剑客行——初相识Matplotlib

队列管理器running状态下无法查看通道

Bold prediction: it will become the core player of 5g

zk配置中心---Config Toolkit配置与使用
随机推荐
Idea view bytecode configuration
Matplotlib剑客行——没有工具用代码也能画图的造型师
Matplotlib swordsman line - layout guide and multi map implementation (Updated)
Matplotlib swordsman Tour - an artist tutorial to accommodate all rivers
MySQL multi column in operation
Customize redis connection pool
以字节跳动内部 Data Catalog 架构升级为例聊业务系统的性能优化
Microservice practice | Eureka registration center and cluster construction
数构(C语言--代码有注释)——第二章、线性表(更新版)
微服务实战|微服务网关Zuul入门与实战
《统计学习方法》——第五章、决策树模型与学习(上)
How to use PHP spoole to implement millisecond scheduled tasks
西瓜书--第五章.神经网络
Don't look for it. All the necessary plug-ins for Chrome browser are here
Chrome浏览器标签管理插件–OneTab
Mathematics in machine learning -- point estimation (I): basic knowledge
Watermelon book -- Chapter 5 neural network
「Redis源码系列」关于源码阅读的学习与思考
Solution to amq4036 error in remote connection to IBM MQ
[go practical basis] how to install and use gin