java实现磁盘文件检索!

来源:互联网 发布:windows版五笔输入法 编辑:程序博客网 时间:2024/03/29 19:40
import java.io.BufferedWriter;import java.io.File;import java.io.FileWriter;import java.io.IOException;import javax.swing.filechooser.FileSystemView;class Threadsearch extends Thread{public String pathString;public Threadsearch(String pathString){this.pathString=pathString;}public void run() {super.run();SearchFilm sFilm = new SearchFilm();try {sFilm.serch(pathString);} catch (IOException e) {e.printStackTrace();}finally{System.out.println("共搜到: "+SearchFile.FileCount+"个文件!\n搜寻结束!");}}}public class SearchFile {public static int FileCount = 0;public static void main(String[] args) throws IOException {Threadsearch t1= new Threadsearch("F:/");Threadsearch t2= new Threadsearch("E:/");Threadsearch t3= new Threadsearch("D:/");t1.start();t2.start();t3.start();}}class SearchFilm {public void serch(String path) throws IOException {int i;File file = new File(path);File listFile[];if (!file.exists()) {System.out.println(path + "不存在!");return;}if (file.isDirectory()) {listFile = file.listFiles();for (i = 0; listFile != null && i < listFile.length; i++) {if (listFile[i].isFile()) {// 是文件筛选,其实应该有过滤器的,只是不懂用了这个不太好的办法if (listFile[i].getCanonicalPath().endsWith(".rmvb")|| listFile[i].getCanonicalPath().endsWith(".rm")|| listFile[i].getCanonicalPath().endsWith(".wmv")|| listFile[i].getCanonicalPath().endsWith(".avi")|| listFile[i].getCanonicalPath().endsWith(".mp4")) {writeData(listFile[i].getCanonicalPath());System.out.println("找到了: " + listFile[i].getCanonicalPath());SearchFile.FileCount++;}} else {// 是目录递归寻找!System.out.println("正在寻找: " + listFile[i].getCanonicalPath());serch(listFile[i].getCanonicalPath() + "/");}}}}/* 这个方法用来写入信息到桌面文件文件 */public synchronized void writeData(String str) throws IOException {FileWriter fw = null; // 用来写入文件FileSystemView fsv = FileSystemView.getFileSystemView();// 取桌面路径File f = new File(fsv.getHomeDirectory() + "/FilmList.txt");try {if (!f.exists()) {f.createNewFile();}fw = new FileWriter(f, true);// ture表示在后面追加一行!BufferedWriter out = new BufferedWriter(fw);out.write(str, 0, str.length()-1);out.newLine();out.close();} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}}


写个多线程搜索视频文件,在写入txt文件时出现乱码!可能是某些问题半角问题!

 

如果不是多线程码就没有事!

如下!

import java.io.BufferedWriter;import java.io.File;import java.io.FileWriter;import java.io.IOException;import javax.swing.filechooser.FileSystemView;public class SearchData {private static int FileCount = 0;public static void main(String[] args) throws IOException {serch("F:/");serch("E:/");serch("D:/");System.out.println("共搜到了" + FileCount + "个文件!");}public static void serch(String path) throws IOException{int i;File file = new File(path);File listFile[];if (!file.exists()) {System.out.println(path + "不存在!");return;}if (file.isDirectory()) {listFile = file.listFiles();for (i = 0; listFile != null && i < listFile.length; i++) {if (listFile[i].isFile()) {// 是文件筛选,其实应该有过滤器的,只是不懂用了这个不太好的办法if (listFile[i].getCanonicalPath().endsWith(".rmvb")|| listFile[i].getCanonicalPath().endsWith(".rm")|| listFile[i].getCanonicalPath().endsWith(".wmv")|| listFile[i].getCanonicalPath().endsWith(".avi")|| listFile[i].getCanonicalPath().endsWith(".mp4")) {writeData(listFile[i].getCanonicalPath());System.out.println("找到了: " + listFile[i].getCanonicalPath());FileCount++;}} else {// 是目录递归寻找!System.out.println("正在寻找: " + listFile[i].getCanonicalPath());serch(listFile[i].getCanonicalPath() + "/");}}}}/* 这个方法用来写入信息到文件 */public static void writeData(String str) throws IOException {FileWriter fw = null; // 用来写入文件FileSystemView fsv = FileSystemView.getFileSystemView();// 取桌面路径File f = new File(fsv.getHomeDirectory() + "/FilmList.txt");try {if (!f.exists()) {f.createNewFile();}fw = new FileWriter(f, true);// ture表示在后面追加一行!BufferedWriter out = new BufferedWriter(fw);out.write(str, 0, str.length());out.newLine();out.close();} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}}


 

 

 

原创粉丝点击