2017-06-20 7 views
-1

それをコピー。これらのファイルを選択して並列処理を開始します。私はGuavaのファイルユーティリティメソッドを使ってファイルを移動しています。グアバのFiles.move()は、ファイルを移動しないか、

Files.move(sourceFile,targetFile,Charsets.UTF_8); 

私は[〜ファイルがorg.apache.commons.io.FileUtils.openInputStream(FileUtils.java:299)で

を読み取ることがないというのが私のTaskSchedulerクラスのエラーを見ていますコモンズ-IO-2.4.jar:org.apache.commons.io.FileUtils.lineIterator(FileUtils.java:1856)で2.4] 〜[コモンズ-IO-2.4.jar:2.4] com.varun.processorで 。 readFile(Processor.java:342)〜[classes/:?]

私の勘はFileですコピーひいては春予定のスレッドにグアバのdocあたりit.Asを読むためにロックを取得できなかったことは、その移動方法は、UNIX上でmvコマンドのように振る舞うと言うが、私はグアバの瓶にコピーとしてコードを参照してください。

enter image description here

誰がo/p個のディレクトリが別の下流のプロセスのためのI/Pである春アプリからUNIXシステム上のファイルを移動するためのより良い方法を提案することができます。グアバでファイルを移動する

+8

それはコピーのみ場合'renameTo'は失敗します。 –

+3

これはmv'がどのように働くか 'もあります。ファイルをインプレースで移動できない場合は、コピーを作成して元のファイルを削除します。 –

答えて

0

が正常に動作します。成功するまでファイルを読み込もうとするだけです。

Iサイズで525メガバイトのファイルを移動させる試験し、グアバは1秒未満でそれを移動させます。

(それが移動されていた前にしながら、ファイルのプロセッサがファイルを開こうとしますので、私は意図的に、ファイルを移動する前に遅延を追加しました)は、例えば以下を参照:

import com.google.common.io.ByteSource; 
import com.google.common.io.Files; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.util.concurrent.Callable; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
import java.util.concurrent.TimeUnit; 
import java.util.logging.Level; 
import java.util.logging.Logger; 


/** 
* <dependency> 
* <groupId>com.google.guava</groupId> 
* <artifactId>guava</artifactId> 
* <version>22.0</version> 
* </dependency> 
*/ 
public class GuavaFileMoveExample { 

    private static final Logger LOGGER = Logger.getLogger("GuavaFileMoveExample"); 

    public static void main(String[] args) throws IOException, InterruptedException { 
     GuavaFileMoveExample a = new GuavaFileMoveExample(); 
     a.doTheWork(); 
    } 

    private void doTheWork() throws InterruptedException { 

     ExecutorService executorService = Executors.newFixedThreadPool(2); 
     executorService.submit(new FileMover()); 
     executorService.submit(new FileProcessor()); 
     executorService.shutdown(); 
     executorService.awaitTermination(10, TimeUnit.SECONDS); 


    } 
} 

class FileMover implements Callable<Void> { 

    private static final Logger LOGGER = Logger.getLogger("FileMover"); 

    @Override 
    public Void call() throws Exception { 
     Thread.sleep(1000); 
     moveFile(); 
     return null; 
    } 

    private void moveFile() { 
     final File sourceFile = new File("/tmp/origin/ideaIU-2016.3-no-jdk.tar.gz"); 
     final File targetFile = new File("/tmp/destination/ideaIU-2016.3-no-jdk.tar.gz"); 

     try { 
      LOGGER.log(Level.INFO, "started moving file"); 
      Files.move(sourceFile, targetFile); 
      LOGGER.log(Level.INFO, "finished moving file"); 
     } catch (IOException e) { 
      LOGGER.log(Level.WARNING, "ioexception while moving file ", e); 
     } 

    } 
} 

class FileProcessor implements Callable<Void> { 

    private static final Logger LOGGER = Logger.getLogger("FileProcessor"); 

    @Override 
    public Void call() throws Exception { 
     readBinaryFile("/tmp/destination/ideaIU-2016.3-no-jdk.tar.gz"); 

     return null; 
    } 

    private byte[] readBinaryFile(String aFileName) { 
     File file = new File(aFileName); 
     ByteSource source = Files.asByteSource(file); 


     byte[] result = null; 
     while (result == null) { 
      try { 
       LOGGER.log(Level.INFO, "started reading file"); 
       result = source.read(); 
       LOGGER.log(Level.INFO, "finished reading file. size: " + result.length); 
      } catch (FileNotFoundException e) { 
       // expected if file is not yet at destination 
      } catch (IOException e) { 
       LOGGER.log(Level.SEVERE, "error reading file", e); 
       break; 
      } 
     } 

     return result; 
    } 
} 
関連する問題