当前位置:网站首页>每天睡觉前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("删除成功!");
}
}
}
边栏推荐
- Micro service practice | introduction and practice of zuul, a micro service gateway
- 西瓜书--第六章.支持向量机(SVM)
- 十年开发经验的程序员告诉你,你还缺少哪些核心竞争力?
- MySQL事务
- Win10 uses docker to pull the redis image and reports an error read only file system: unknown
- Taking the upgrade of ByteDance internal data catalog architecture as an example, talk about the performance optimization of business system
- 机器学习实战:《美人鱼》属于爱情片还是动作片?KNN揭晓答案
- Difference between redis serialization genericjackson2jsonredisserializer and jackson2jsonredisserializer
- Microservice practice | teach you to develop load balancing components hand in hand
- 【Go实战基础】gin 如何验证请求参数
猜你喜欢
Typeerror: X () got multiple values for argument 'y‘
破茧|一文说透什么是真正的云原生
「Redis源码系列」关于源码阅读的学习与思考
Matplotlib剑客行——布局指南与多图实现(更新)
Chrome视频下载插件–Video Downloader for Chrome
【Go实战基础】如何安装和使用 gin
Number structure (C language) -- Chapter 4, compressed storage of matrices (Part 2)
Servlet全解:继承关系、生命周期、容器和请求转发与重定向等
Complete solution of servlet: inheritance relationship, life cycle, container, request forwarding and redirection, etc
Flink - use the streaming batch API to count the number of words
随机推荐
京东面试官问:LEFT JOIN关联表中用ON还是WHERE跟条件有什么区别
Cartoon rendering - average normal stroke
Watermelon book -- Chapter 6 Support vector machine (SVM)
Chrome browser plug-in fatkun installation and introduction
Matplotlib剑客行——没有工具用代码也能画图的造型师
机器学习实战:《美人鱼》属于爱情片还是动作片?KNN揭晓答案
From concept to method, the statistical learning method -- Chapter 3, k-nearest neighbor method
Win10 uses docker to pull the redis image and reports an error read only file system: unknown
Chrome浏览器标签管理插件–OneTab
Timed thread pool implements request merging
Insight into cloud native | microservices and microservice architecture
Idea view bytecode configuration
There is a problem with MySQL installation (the service already exists)
[go practical basis] how can gin get the request parameters of get and post
Learn combinelatest through a practical example
MySQL error: unblock with mysqladmin flush hosts
Solutions to Chinese garbled code in CMD window
Bold prediction: it will become the core player of 5g
自定義Redis連接池
微服务实战|原生态实现服务的发现与调用