2017-04-04 32 views
0

我正在处理一个项目,该项目使用四个线程来复制文件。使用线程从列表中复制文件

我创建列表并存储要复制的文件的名称。 我想使用这4个线程一起工作,但我真的不知道如何使它发生。

public class CopyingFiles implements Runnable 
{ 
static File source = new File("C:\\test\\1\\"); 
static File dest = new File("C:\\test\\2\\"); 

@Override 
public void run() 
{ 
    try 
    { 
     CopyingFromList(source, dest); 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
    catch (InterruptedException e) 
    { 
     e.printStackTrace(); 
    } 
} 

public static void CopyFile(File sourceFile, File destination) throws IOException 
{ 

    InputStream inputStream = null; 
    OutputStream outputStream = null; 

    try { 
     inputStream = new FileInputStream(sourceFile); 
     outputStream = new FileOutputStream(destination); 

     byte[] buffer = new byte[1024]; 


     int length; 
     while ((length = inputStream.read(buffer)) > 0) 
     { 
      outputStream.write(buffer, 0 ,length); 
     } 
    } finally { 
     if(inputStream != null) 
     { 
      inputStream.close(); 
     } 

     if(inputStream != null) 
     { 
      outputStream.close(); 
     } 
    } 
} 

public static void CopyingFromList(File source, File dest) throws IOException, InterruptedException 
{ 
    List<String> fileList = FilesList.CreateFilesList(source); 

    for(String file : fileList) 
    { 
     System.out.println(Thread.currentThread().getName() + " > " + FilesList.DestinationOfFile(source) + file + " > " + FilesList.DestinationOfFile(dest) + file); 
     CopyFile(new File(FilesList.DestinationOfFile(source) + file), new File(FilesList.DestinationOfFile(dest) + file)); 
    } 
} 
} 

,二类

public class FilesList 
    { 
    static File source = new File("C:\\test\\1\\"); 
    static File source1 = new File("C:\\test\\3\\"); 
    static File dest = new File("C:\\test\\2\\"); 
    static File dest1 = new File("C:\\test\\4\\"); 


    public static List<String> CreateFilesList(File source) throws InterruptedException, IOException 
    { 
     List<String> fileList = new ArrayList<>(Arrays.asList(source.list())); 

     return fileList; 
    } 

    public static String DestinationOfFile(File source) 
    { 
     return new String(source + "\\"); 
    } 

    public static void PrintWholeList(File source) throws IOException, InterruptedException 
    { 
     List<String> fileList = CreateFilesList(source); 
     for(String file : fileList) 
     { 
      System.out.println(DestinationOfFile(source) + file); 
     } 
    } 


    public static void main(String []args) throws IOException, InterruptedException 
    { /* 
     //CopyingFiles.CopyFile(new File(source+"\\file1.txt"), new File(dest+"\\file1.txt")); 
     //CopyingFiles.CopyingFromList(source,dest); 

     CopyingFiles t1 = new CopyingFiles(); 
     CopyingFiles t2 = new CopyingFiles(); 
     CopyingFiles t3 = new CopyingFiles(); 
     CopyingFiles t4 = new CopyingFiles(); 

     t1.start(); 
     t2.start(); 
     t3.start(); 
     t4.start(); 
     */ 
     ExecutorService executorService = Executors.newFixedThreadPool(5); 
     System.out.println(Thread.activeCount()); 

     executorService.submit(() -> { 
      try 
      { 
       CopyingFiles.CopyingFromList(source,dest); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
      catch (InterruptedException e) 
      { 
       e.printStackTrace(); 
      } 
      finally 
      { 
       executorService.shutdown(); 
      } 
     }); 
    } 
} 

谁能帮助我,或显示一些其他的方式来解决我的问题。

+0

你在这段代码中面临的错误是什么? – Omore

+0

P.S.对不起,如果我做的东西不对,这是我的第一篇文章 –

+2

这很可能是磁盘I/O是这里的瓶颈。为什么复制4线程,如果它与1线程复制一样快? –

回答

0

我实际上没有得到什么问题。这一次对我的作品(和它几乎是你的程序,只是清理了一下):

package multicp; 

import java.io.IOException; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.concurrent.Callable; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 

import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES; 

/** 
* Copy files using threads. 
*/ 
public class Multicp { 
    public static void main(String[] args) { 
     // List of source/dest pairs ("tasks") 
     List<CopierCallable<Void>> opsList = new ArrayList<>(); 
     opsList.add(new CopierCallable<>(Paths.get("f1src.dat"), Paths.get("f1dest.dat"))); 
     opsList.add(new CopierCallable<>(Paths.get("f2src.dat"), Paths.get("f2dest.dat"))); 
     opsList.add(new CopierCallable<>(Paths.get("f3src.dat"), Paths.get("f3dest.dat"))); 
     opsList.add(new CopierCallable<>(Paths.get("f4src.dat"), Paths.get("f4dest.dat"))); 

     ExecutorService execSvc = Executors.newFixedThreadPool(2); // 4 in your case. 2 is just for testing 
     try { 
      execSvc.invokeAll(opsList); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } finally { 
      execSvc.shutdown(); 
     } 
    } 

} 

/** 
* Performs actual copying from one source to one destination. 
*/ 
class CopierCallable<Void> implements Callable<Void> { 
    private Path pathFrom; 
    private Path pathTo; 

    public CopierCallable(Path pathFrom, Path pathTo) { 
     this.pathFrom = pathFrom; 
     this.pathTo = pathTo; 
    } 

    @Override 
    public Void call() { 
     try { 
      // REPLACE_EXISTING is destructive, uncomment at your own risk 
      Files.copy(pathFrom, pathTo, COPY_ATTRIBUTES /*, REPLACE_EXISTING*/); 
      System.out.println(pathFrom + " copied"); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 
} 

我可以看到文件同时被复制成组(由2进行测试;具有4取代,但它会使初始化代码较大,无利可图)。