文件操作

package com.lingdian.search;


import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;


import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;


public class MainSearch {


public static void main(String[] args) {
File file = new File("E:\\2015\\07\\all");
readFile(file);
}
public static boolean isDoFile(File file){
boolean flag = false;
if(file.isDirectory()){
String[] fileNames = file.list();
for(String name:fileNames){
if(name.indexOf("xls")>0){
flag = true;
break;
}
}
}
return flag;
}

public static void readFile(File f){
if(f.isDirectory()){
File[] t = f.listFiles();
   for(int i=0;i<t.length;i++){
    if(isDoFile(t[i])){
    dosearch(t[i]);
    }
           /*    if(t[i].isDirectory()){
                   readFile(t[i]);
               }*/
               else{
                   System.out.println(t[i].getName());
               }
           }
}else{
String fileName = f.getName();
System.out.println(fileName);
}
}
private static void dosearch(File file) {
System.out.println("search the file in "+file.getName());
Set<String> idSet = null;
String[] pics = null;
if(file.isDirectory()){
File[] t = file.listFiles();
for(File f:t){
if(isExcel(f)){
idSet = readExcel(f);
}
if(f.isDirectory()){
pics = f.list();
}
if(idSet!=null&&pics!=null){
searchDif(pics,idSet);
}

}
}
}
public static boolean isExcel(File excel){
return excel.getName().indexOf("xls")>0;
}
public static void searchDif(String[] picNames,Set<String> ids){
for(String picName:picNames){
picName = removeTag(picName);
if(!ids.contains(picName)){
System.out.println("not found the pic:"+picName);
}else{
System.out.println("found the pic:"+picName);
ids.remove(picName);
}
}
}
public static String removeTag(String fileName){
String s = fileName.substring(0, fileName.lastIndexOf("."));
return s;
}
public static Set<String> readExcel(File file){
Set<String> idSet = new HashSet<String>();
Workbook workbook;
try {
workbook = Workbook.getWorkbook(file);
Sheet sheet = workbook.getSheet(0);
int length = sheet.getRows();
for(int i=1;i<length;i++){
Cell a1 = sheet.getCell(0,i);
String a = a1.getContents();
idSet.add(a);
}

} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return idSet;
}
}

你可能感兴趣的:(文件操作)