一.Path
Path表示的是一个路径或带有路径的文件。
使用(Paths类提供的)静态get方法获取一个路径,接受String或URL作为参数。
提供了几种常用的方法:
getFileName() getParent() getRoot() getNameCount()
这四种看命名就能很容易的猜出用法。
getName(int index) 获取每一层(0~~n-1)的路径名
resolve(String other) 组合路径。如果other是绝对路径则返回。
resolveSibling(String ohter) 与上记不同的是,与父路径结合,返回一个兄弟路径。
relativize(Path other) 回other相对于this的相对路径
扩展:
获取一个路径的3种方法:
Path path = Paths.get("C:\\Users\\TalkServer.java");
System.out.println(path.toString());
File file = new File("C:\\Users\\TalkServer.java");
Path path2 = file.toPath();
System.out.println(path2.toString());
Path path3 = FileSystems.getDefault().getPath("C:\\Users", "TalkServer.java");
System.out.println(path3.toString());
三.Files
java.nio.file.Files 类是普通文件操作便捷。其中所有方法都是静态方法。
以下对方法进行简单分类:
1.读写文件
byte[] readAllBytes(path)将文件当作字节读入。
//将文件当作字符串读入
byte[] bytes = Files.readAllBytes(path);
String content = new String(bytes, charset);
List<String> readAllLines(path)
将文件当作行序列读入。
向文件中写入给定内容
使用流处理器进行读写操作读写
InputStream newInputStream(Path path)
OutputStream newOutputStream(Path path)
BufferedReader newBufferedReader(Path path)
BufferedWriter newBufferedWriter(Path path)
2.复制,移动和删除文件
copy(fromPath, toPath [, option])
move(fromPath, toPath [, option])
option为: StandardCopyOption.REPLACE_EXISTING 覆盖已有的目标路径;StandardCopyOption.COPY_ATTRIBUTES复制所有的文件属 性;StandardCopyOption.ATOMIC_MOVE移动成功。否则源文件保持不变。
void delete(path)
boolean deleteIfExists(path)
3.创建文件和目录
createFile(path)创建文件
createDirectory(path)创建目录
createDirectorise(path) 创建目录(会自动创建中间目录)
4.获取文件信息
exist() isHidden() size()
readAttributes()获取文件的基本属性集。
5.迭代目录中的文件
File类中有一个方法,用来获取一个目录中的所有文件构成的数组(listFIles())。但性能较低。
Files类由此设计了一个方法,它可以产生一个Iterable对象。
1.获取目录流中的文件
newDirectoryStream(Pah)
newDirectoriStream(Pah, String glob)glob限制匹配条件
获取给定目录中可以遍历所有文件的迭代器
2.遍历所有子孙,并添加一个访问器
walkFileTree(path, visitor)
想访问某个子孙成员时,调用以下方法
在遇到一个文件或目录:visitFile()
目录被处理前: preVisitResult()
目录被处理后: postVisitResult()
访问时发生错误: visitFileFailer()
*在以上四种情况下,都可以指定以下操作
继续访问: FileVisitResult.CONTINUE
继续访问(无视这个目录的任何项)
FileVisitResult.SKIP_SUBTREE
继续访问(无视这个文件的兄弟文件)
FileVisitResult.SKIP_SIBLINGS
终止访问: TERMINATE
Files.walkFileTree(dir, new SimpleFileVisitor<Path>()){
<span style="white-space:pre"> </span>public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException{
<span style="white-space:pre"> </span>if(attrs.isDirectory())
<span style="white-space:pre"> </span>System.out.println(path);
<span style="white-space:pre"> </span>return FileVisitResult.CONTINUE;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public FileVisitResult visitFileFailed(Path, IOException exc) throws IOExceptioin{
<span style="white-space:pre"> </span>return FileVisitResult.CONTINUE;
<span style="white-space:pre"> </span>}
}
扩展:
以下是使用File类listFiles遍历的方法:
File fileDir = new File("F:\\DownLoad\\");
findFiles(fileDir);
public static void findFiles(File fileDir) {
if(fileDir.isDirectory()){
File[] file = fileDir.listFiles();
try{
<span style="white-space:pre"> </span> for(int i=0; i<file.length; i++){
if(file[i].isDirectory())
findFiles(file[i]);
}
}catch(Exception e){
}
}
<span style="white-space:pre"> </span>}