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);//讀取文件內(nèi)的內(nèi)容
String line=null;
while((line = reader.readLine())!=null) {
if(line.equals("account"))//若讀取的內(nèi)容等于“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 的注冊關(guān)系。WatchKey 在 Watchable 對象向 WatchService 注冊的時候被創(chuàng)建。它是 Watchable 和 WatchService 之間的關(guān)聯(lián)類。
以上就是本次介紹的關(guān)于Java文件與類動手動腦實例的全部知識點,感謝大家的學習和對腳本之家的支持。
相關(guān)文章
解析python調(diào)用函數(shù)加括號和不加括號的區(qū)別
這篇文章主要介紹了python調(diào)用函數(shù)加括號和不加括號的區(qū)別,不帶括號時,調(diào)用的是這個函數(shù)本身 ,是整個函數(shù)體,是一個函數(shù)對象,不須等該函數(shù)執(zhí)行完成,具體實例代碼跟隨小編一起看看吧2021-10-10
小議Python中自定義函數(shù)的可變參數(shù)的使用及注意點
Python函數(shù)的默認值參數(shù)只會在函數(shù)定義處被解析一次,以后再使用時這個默認值還是一樣,這在與可變參數(shù)共同使用時便會產(chǎn)生困惑,下面就來小議Python中自定義函數(shù)的可變參數(shù)的使用及注意點2016-06-06
Python讀取配置文件-ConfigParser的二次封裝方法
這篇文章主要介紹了Python讀取配置文件-ConfigParser的二次封裝方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
Python 格式化輸出_String Formatting_控制小數(shù)點位數(shù)的實例詳解
在本篇文章里小編給大家整理了關(guān)于Python 格式化輸出_String Formatting_控制小數(shù)點位數(shù)的實例內(nèi)容,需要的朋友們參考下。2020-02-02

