I have 30 files each 1GB in size. From one place they need to be moved to another in parallel.

Roughly speaking, it works like this:

String[] files = new String[] {"file1.cat", "file2.cat"...} for(int i=0; i < files.length; i++) { new Thread(new Runnable() { Files.move(files[i], "target" + i); }).start(); } 

How do I pause the move? Realize the move through the stream?

  • What kind of pause is it? Stop moving the file and continue later? - Eugene Kirin
  • And what could be another pause? I meant to pause the move and continue, not to restart after the pause was completed. - lampa
  • what does moving mean? copying? or moving ? - Ep1demic
  • By moving, I mean the Files.move() method, it is in a piece of example code. And it works, apparently, through copying and deleting. - lampa
  • one
    @ Ep1demic Never solved such a problem, but it seems that java does not provide a convenient mechanism for pausing the copy process. I advise you to implement copying (then deleting the old file) via FileChannel, it is well suited for this. There you can easily stop the process. - Eugene Kirin

1 answer 1

Did so. For now, see what happens with speed.

 public class FileMove { private static boolean globalPause = false; /** * Снять/установить общую паузу * @param type true установить, false снять */ public static void setGlobalPause(boolean type) { globalPause = type; } /** * Перемещение файла * @param from откуда * @param to куда * @throws IOException ошибка копирования * @throws InterruptedException ошибка паузы */ public static void copy(File from, File to) throws IOException, InterruptedException { if(to.exists()) { to.delete(); } FileInputStream inFile = new FileInputStream(from); FileOutputStream outFile = new FileOutputStream(to); FileChannel inChannel = inFile.getChannel(); FileChannel outChannel = outFile.getChannel(); long bufferSize = 8 * 1024; long pos = 0; long count; long size = inChannel.size(); while (pos < size) { if(globalPause) { Thread.sleep(100); continue; } count = size - pos > bufferSize ? bufferSize : size - pos; pos += inChannel.transferTo(pos, count, outChannel); } inFile.close(); outFile.close(); } /** * Перемещение файла * @param from откуда * @param to куда * @throws IOException ошибка копирования * @throws InterruptedException ошибка паузы */ public static void move(File from, File to) throws IOException, InterruptedException { copy(from, to); from.delete(); } }