1. 程式人生 > >多執行緒實現檔案在多層目錄中查詢及拷貝到多層目錄

多執行緒實現檔案在多層目錄中查詢及拷貝到多層目錄

在實現建立多級目錄後,遇到了檔案分類複製的問題。。。左思右想,最後覺得還是寫程式碼實現比較快,畢竟我比較懶。。。不要問我為要寫那麼多find方法來匹配路徑。。。畢竟腦子不夠用。如果是比較重要的檔案,不推薦多執行緒去實現,雖然用了同步,但還是有可能漏掉某個檔案。還是老老實實的在main方法中,一個一個地呼叫find方法吧,這樣比較安全。

//分層查詢並複製要求檔案

package Copy;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;




public class Scanner implements Runnable{
static String path;
static String name;
static String path1;

public synchronized static void find()throws IOException{
    for(int i=1;i<34;i++){
    path="E:\\資料\\01_基礎班_最新北京2017\\day"+i+"\\day"+i+"\\day"+i+"_source";
    path1="E:\\JavaSE\\Day"+i+"\\文件\\";
   
    //System.out.println(path);
    File file = new File(path);
if(!file.exists()){
System.out.println("檔案路徑不存在");
return;
}
File[] f = file.listFiles();
for (int j = 0; j < f.length; j++) {
File fs = f[j];
if (fs.isDirectory()) {
System.out.println(fs.getName() + " [目錄]");
} else {
//System.out.println(fs.getName());
name=fs.getName();
if(fs.getName().endsWith(".doc")||fs.getName().endsWith(".docx")){
//System.out.println(path1+name);
copy(new File(path+"\\"+name),new File(path1+name));
    }
}

}
    

}


    }

public synchronized static void find1()throws IOException{
    for(int i=1;i<34;i++){
    path="E:\\資料\\01_基礎班_最新北京2017\\day"+i+"\\day"+i;
    path1="E:\\JavaSE\\Day"+i+"\\文件\\";
   
    //System.out.println(path);
    File file = new File(path);
if(!file.exists()){
System.out.println("檔案路徑不存在");
return;
}
File[] f = file.listFiles();
for (int j = 0; j < f.length; j++) {
File fs = f[j];
if (fs.isDirectory()) {
System.out.println(fs.getName() + " [目錄]");
} else {
//System.out.println(fs.getName());
name=fs.getName();
if(fs.getName().endsWith(".doc")||fs.getName().endsWith(".docx")){
//System.out.println(path1+name);
copy(new File(path+"\\"+name),new File(path1+name));
    }
}

}
    

}


    }

public synchronized static void find2()throws IOException{
    for(int i=1;i<34;i++){
    path="E:\\資料\\01_基礎班_最新北京2017\\day"+i+"\\day"+i+"\\day"+i+"_source";
    path1="E:\\JavaSE\\Day"+i+"\\PPT\\";
   
    //System.out.println(path);
    File file = new File(path);
if(!file.exists()){
System.out.println("檔案路徑不存在");
return;
}
File[] f = file.listFiles();
for (int j = 0; j < f.length; j++) {
File fs = f[j];
if (fs.isDirectory()) {
System.out.println(fs.getName() + " [目錄]");
} else {
//System.out.println(fs.getName());
name=fs.getName();
if(fs.getName().endsWith(".ppt")||fs.getName().endsWith(".pptx")){
//System.out.println(path1+name);
copy(new File(path+"\\"+name),new File(path1+name));
    }
}

}
    

}


    }

public synchronized static void find3()throws IOException{
    for(int i=1;i<34;i++){
    path="E:\\資料\\01_基礎班_最新北京2017\\day"+i+"\\day"+i;
    path1="E:\\JavaSE\\Day"+i+"\\PPT\\";
   
    //System.out.println(path);
    File file = new File(path);
if(!file.exists()){
System.out.println("檔案路徑不存在");
return;
}
File[] f = file.listFiles();
for (int j = 0; j < f.length; j++) {
File fs = f[j];
if (fs.isDirectory()) {
System.out.println(fs.getName() + " [目錄]");
} else {
//System.out.println(fs.getName());
name=fs.getName();
if(fs.getName().endsWith(".ppt")||fs.getName().endsWith(".pptx")){
//System.out.println(path1+name);
copy(new File(path+"\\"+name),new File(path1+name));
    }
}

}
    

}


    }

public synchronized static void find4()throws IOException{
    for(int i=1;i<34;i++){
    path="E:\\資料\\01_基礎班_最新北京2017\\day"+i+"\\day"+i+"\\day"+i+"_source";
    path1="E:\\JavaSE\\Day"+i+"\\筆記\\";
   
    //System.out.println(path);
    File file = new File(path);
if(!file.exists()){
System.out.println("檔案路徑不存在");
return;
}
File[] f = file.listFiles();
for (int j = 0; j < f.length; j++) {
File fs = f[j];
if (fs.isDirectory()) {
System.out.println(fs.getName() + " [目錄]");
} else {
//System.out.println(fs.getName());
name=fs.getName();
if(fs.getName().endsWith(".md")){
//System.out.println(path1+name);
copy(new File(path+"\\"+name),new File(path1+name));
    }
}

}
    

}


    }


public synchronized static void copy(File src,File desc) throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
int len = 0;
byte[] bytes = new byte[1024];//1024是1kb
while((len=bis.read(bytes))!=-1){
bos.write(bytes,0,len);
}
bos.close();
bis.close();
}



@Override
public void run() {
// TODO Auto-generated method stub
try {
find();
find1();
find2();
find3();
find4();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}


}

package Copy;


public class TestForScanner {


public static void main(String[] args) {
Scanner sc = new Scanner();
Thread t0 = new Thread(sc);
Thread t1 = new Thread(sc);
Thread t2 = new Thread(sc);
Thread t3 = new Thread(sc);

t0.start();
t1.start();
t2.start();
t3.start();


}