0

正常に動作しているフォルダーとファイルをコピーしようとしていますが、単一のフォルダーをフィルター処理して残りのフォルダーをコピーする方法について助けが必要です。たとえば、(C:\vehicle\carsfolder と C:\vehicle\truckfolder) に carsfolder と truckfolder のようなディレクトリがあります。以下のコードを使用すると、carsfolder と trackfolder の両方がコピーされますが、carsfolder のみをコピーしたかったのです。どうやってやるの。(Swing と Java 1.6 を使用)

 class CopyTask extends SwingWorker<Void, Integer>
                 {
                   private File source;
                   private File target;
                   private long totalBytes = 0;
                   private long copiedBytes = 0;
                   public CopyTask(File src, File dest)
                 {
                    this.source = src;
                    this.target = dest;
                    progressAll.setValue(0);

                  }
         @Override
         public Void doInBackground() throws Exception
                 {

                    ta.append("Retrieving info ... ");  //append to TextArea
                    retrieveTotalBytes(source);
                    ta.append("Done!\n");
                    copyFiles(source, target);
                    return null;
                 }
         @Override
       public void process(List<Integer> chunks)
              {
                for(int i : chunks)
              {
                         }
        }
         @Override
         public void done()
                    {
                      setProgress(100);
                      }
        private void retrieveTotalBytes(File sourceFile)
                    {
                    try
                    {
                      File[] files = sourceFile.listFiles();
                      for(File file : files)
                    {
                     if(file.isDirectory()) retrieveTotalBytes(file);
                     else totalBytes += file.length();
                     }
                  }
                 catch(Exception ee)
                   {
                                            }
               }
    private void copyFiles(File sourceFile, File targetFile) throws IOException
                   {
                     if(sourceFile.isDirectory())
                   {
         try{
                if(!targetFile.exists()) targetFile.mkdirs();
                String[] filePaths = sourceFile.list();
                for(String filePath : filePaths)
               {
                File srcFile = new File(sourceFile, filePath);
                File destFile = new File(targetFile, filePath);
                copyFiles(srcFile, destFile);
               }
          }
        catch(Exception ie)
              {
                                   }
         }
    else
             {
                try
                     {
                        ta.append("Copying " + sourceFile.getAbsolutePath() + " to " + targetFile.getAbsolutePath() );
                        bis = new BufferedInputStream(new FileInputStream(sourceFile));
                        bos = new BufferedOutputStream(new FileOutputStream(targetFile));
                        long fileBytes = sourceFile.length();
                        long soFar = 0;
                        int theByte;
             while((theByte = bis.read()) != -1)
                       {
                         bos.write(theByte);
                         setProgress((int) (copiedBytes++ * 100 / totalBytes));
                         publish((int) (soFar++ * 100 / fileBytes));
                       }
                          bis.close();
                          bos.close();
                          publish(100);
                          ta.append(" Done!\n");
                      }
           catch(Exception excep)
                            {
                              setProgress(0);
                              bos.flush();
                              bis.close();
                              bos.close();

                            }
          finally{
                    try {
                          bos.flush();
                        } 
                    catch (Exception e) {
                         }
                    try {
                            bis.close();
                        }
                    catch (Exception e) {
                        }
                   try {
                       bos.close();
                       } 
                   catch (Exception e) {
                    }
                }
}
}
}
4

1 に答える 1

1

除外するファイルとディレクトリを指定する正規表現または正規表現のリストを導入できますか?

たとえば、truckfolder を除外するには、 のような「除外」正規表現を使用し"C:\\vehicle\\truckfolder.*"ます。

次に、コードで何かをコピーする前に、ソースファイルの絶対パスが除外正規表現と一致しないことを確認してください。

于 2013-01-30T20:35:35.503 に答える