Path 操作
1.删除文件
2.遍历目录,不包括子目录的文件
Path dir = Paths.get("F:\\LZ\\pdf")
DirectoryStream<Path> stream = Files.newDirectoryStream(dir)
for(Path path : stream){
System.out.println(path.getFileName())
}
2.遍历目录,及其子目录下的文件
Path dir = Paths.get("F:\\LZ\\pdf");
Files.walkFileTree(dir,new SimpleFileVisitor<Path>(){
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
if(file.toString().endsWith(".pdf")){
System.out.println(file.getFileName());
}
return super.visitFile(file, attrs);
}
});
3.创建多级目录
Path dir = Paths.get("F:\\LZ\\xx\\dd");
Files.createDirectories(dir);
4.创建文件, 不存在则抛出异常
Path dir = Paths.get("F:\\LZ\\xx\\dd\\1.txt");
Files.createFile(dir);
5.文件的复制
Path src = Paths.get("F:\\LZ\\xx\\dd\\1.txt");
Path target = Paths.get("F:\\LZ\\xx\\dd\\2.txt");
//REPLACE_EXISTING:文件存在,就替换
Files.copy(src, target,StandardCopyOption.REPLACE_EXISTING);
6.一行一行读取文件
Path src = Paths.get("F:\\LZ\\xx\\dd\\1.txt")
BufferedReader reader = Files.newBufferedReader(src,StandardCharsets.UTF_8)
String line
while((line=reader.readLine()) != null){
System.out.println(line)
}
reader.close()
7.写入字符串
Path src = Paths.get("F:\\LZ\\xx\\dd\\1.txt")
BufferedWriter writer = Files.newBufferedWriter(src, StandardCharsets.UTF_8
,StandardOpenOption.APPEND)
writer.write("hello word Path")
writer.close()
8.二进制 读写 与字符串类似
- 一个方法直接去取字符串 和 二进制流
Path src = Paths.get("F:\\LZ\\xx\\dd\\1.txt");
for(String line : Files.readAllLines(src)){
System.out.println(line);
}
byte[] bytes = Files.readAllBytes(src);
10.监测是否 目录下的 文件,目录 被修改,创建,或删除.
![这里写图片描述](https://img-blog.csdn.net/20160617093407456)
11.读取文件的 最后 1000 个字符
![这里写图片描述](https://img-blog.csdn.net/20160617093434862)
官方API:
http://docs.oracle.com/javase/7/docs/api/
官方文档:http://docs.oracle.com/javase/7/docs/index.html
在我学习使用lucene5.0的时候发现用到了Path,怎么点都点不进去,后来在网上查到是JDK7里边的东西,然后自己去获取Path对象却又不知道怎么获取,所以就去查找如何获取Path,就找到了这篇文章,亲自示范通过,然后发表一下希望能有所帮助。
例子如下:
- package com.darren.test.jdk7;
-
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.IOException;
- import java.nio.charset.Charset;
- import java.nio.file.FileSystems;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
-
-
-
-
- public class PathTest {
- public static void main(String[] args) {
-
- Path path = FileSystems.getDefault().getPath("F:/test/jdk7", "test.txt");
-
- System.out.println(path.getNameCount());
-
-
- File file = new File("F:/test/jdk7/test.txt");
- Path pathOther = file.toPath();
-
- System.out.println(path.compareTo(pathOther));
-
-
- Path path3 = Paths.get("F:/test/jdk7", "test.txt");
- System.out.println(path3.toString());
-
-
- Path path4 = Paths.get("F:/test/jdk7");
- System.out.println("path4: " + path4.resolve("test.txt"));
- System.out.println("--------------分割线---------------");
-
- try {
- if (Files.isReadable(path)) {
-
-
- BufferedReader br = Files.newBufferedReader(path, Charset.defaultCharset());
-
- String line = "";
- while ((line = br.readLine()) != null) {
- System.out.println(line);
- }
- } else {
- System.err.println("cannot readable");
- }
- } catch (IOException e) {
- System.err.println("error charset");
- }
- }
- }
运行结果:
- 3
- 0
- F:\test\jdk7\test.txt
- path4: F:\test\jdk7\test.txt
- --------------分割线---------------
- I am Darren