Java文件與類動手動腦實例詳解
動手動腦1:
使用Files. walkFileTree()找出指定文件夾下所有大于指定大?。ū热?M)的文件。
package classJava;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.EnumSet;
public class titletwo implements FileVisitor<Object> {
private long accepted_size;
public void titletwo(String glob,long accepted_size) {
FileSystems.getDefault().getPathMatcher("glob:" +glob);
this.accepted_size=accepted_size;
}
void search(Path file) throws IOException {
long size = (Long) Files.getAttribute(file, "basic:size");
if(size ==accepted_size) {
System.out.println(file);
}
}
@Override
public FileVisitResult postVisitDirectory(Object dir, IOException exc)throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult preVisitDirectory(Object dir, BasicFileAttributes attrs)throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Object file, BasicFileAttributes attrs)throws IOException {
search((Path) file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Object file, IOException exc)throws IOException {
return FileVisitResult.CONTINUE;
}
public static void main(String[] args) throws IOException{
String glob= "*.jpg";
long size = 28672;
Path fileTree = Paths.get("D:/");
titletwo walk=new titletwo();
EnumSet<FileVisitOption> opts=EnumSet.of(FileVisitOption.FOLLOW_LINKS);
System.out.println("D盤中大小等于28672字節(jié)的文件有");
Files.walkFileTree(fileTree, opts, Integer.MAX_VALUE, walk);
}
}
使用Files. walkFileTree()找出指定文件夾下所有擴展名為.txt和.java的文件。
package classJava;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
public class titletwo {
public static void main(String args[]) throws IOException {
String glob = "glob:**/*.{java,txt}";
String path = "D:/";
match(glob, path);
}
public static void match(String glob, String location) throws IOException {
final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher( glob);
Files.walkFileTree(Paths.get(location), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path path,
BasicFileAttributes attrs) throws IOException {
if (pathMatcher.matches(path)) {
System.out.println(path);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc)
throws IOException {
return FileVisitResult.CONTINUE;
}
});
}
}
使用Files. walkFileTree()找出指定文件夾下所有包容指定字符串的txt文件。
package classJava;
import java.io.IOException;
import java.io.*;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
public class titletwo {
public static void main(String args[]) throws IOException {
String glob = "glob:**/*.txt";
String path = "D:\\wenjian";
match(glob, path);
}
public static void match(String glob, String location) throws IOException {
final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher( glob);
Files.walkFileTree(Paths.get(location), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path path,
BasicFileAttributes attrs) throws IOException {
if (pathMatcher.matches(path)) {
BufferedReader reader =Files.newBufferedReader(path);//讀取文件內的內容
String line=null;
while((line = reader.readLine())!=null) {
if(line.equals("account"))//若讀取的內容等于“account"則輸出文件名
{
System.out.println(path);
break;
}
}
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc)
throws IOException {
return FileVisitResult.CONTINUE;
}
});
}
}
動手動腦2:
java.nio.file.WatchService文件系統(tǒng)監(jiān)視服務的接口類,它的具體實現(xiàn)由監(jiān)視服務提供者負責加載。
java.nio.file.Watchable 實現(xiàn)了 java.nio.file.Watchable 的對象才能注冊監(jiān)視服務 WatchService。java.nio.file.Path實現(xiàn)了 watchable 接口,后文使用 Path 對象注冊監(jiān)視服務。
java.nio.file.WatchKey 該類代表著 Watchable 對象和監(jiān)視服務 WatchService 的注冊關系。WatchKey 在 Watchable 對象向 WatchService 注冊的時候被創(chuàng)建。它是 Watchable 和 WatchService 之間的關聯(lián)類。
以上就是本次介紹的關于Java文件與類動手動腦實例的全部知識點,感謝大家的學習和對腳本之家的支持。
相關文章
基于Mediapipe+Opencv實現(xiàn)手勢檢測功能
mediaPipe,他就是一個集成好的包括人臉關鍵位點識別、身體關鍵位點識別、手部關鍵位點識別的一個包或者庫,直接調用就能夠得到它的關鍵位點信息,本文給大家介紹Mediapipe+Opencv實現(xiàn)手勢檢測功能,感興趣的朋友一起看看吧2022-01-01
Python中Pickling和Unpickling的區(qū)別詳解
在本文中,我們將探討 Python 中 pickling 和 unpickling 之間的主要區(qū)別,我們將詳細討論 Python pickling 和 unpickling 的概念,包括它們的目的、語法、用法以及安全可靠的 pickling 和 unpickling 操作的注意事項,需要的朋友可以參考下2023-09-09
Python split() 函數(shù)拆分字符串將字符串轉化為列的方法
今天小編就為大家分享一篇Python split() 函數(shù)拆分字符串將字符串轉化為列的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07

