Good day.
There is a code:

package ru.copy; public class workProgram { public static void main(String[] args) throws IOException { //МОЙ код для копирования данных } } 

My program copies files from one folder to another. The fact is that files will be periodically poured into the source folder, this will happen for 15-20 hours and you need to copy ALL the files. In addition, these files in the source folder will be there for 1 hour, and then deleted (and therefore need to copy them from there).

How to make a permanent code execution and shutdown procedure?

The simplest thing is to make an eternal cycle:

 public class workProgram { public static void main(String[] args) throws IOException { int a = 1; while (true) { //МОЙ код для копирования данных } } } 

Then the program goes into the eternal cycle, it works and the files are copied. But how to make the correct stop of such a code? And no more accurate solutions?

Yes, I tried launching by time intervals:

  Timer timer = new Timer(); TimerTask timerTask = new TimerTask() { public void run() { //мой код } }; timer.schedule(timerTask, 5000, 10000); //первое число - когда он запуститься, второе - через сколько будет повторяться 

This is incorrect, because the procedure may not have time to complete by the required time and a separate thread will start, which can lead to an error, and again - how to stop it?

PS probably, in my writing style, you understood that I was a beginner, so I beg you to give detailed answers with comments (I tried to ask the most clear question).

  • Check after copying a file, if there are new files to copy, if not, then stop the execution flow, if there is, copy the file and repeat the procedure - Vadim Prokopchuk
  • Can I have code example? - Alex
  • Does the program just copy? Why do you write it in Java? - Mikhail Vaysman
  • I think it is easier to solve it with an external script via a cron job or something like that - Barmaley
  • Looks into the directory, finds the necessary files by mask, takes the first ten, copies, immediately deletes them in the source folder, archives these dozens of files in the recipient's folder, then deletes the copied files. Going to the second round takes the next 10 files and a new one. When the files run out it goes to the OTHER source folder and does the same. Only 4 source folders. I do it on java because this is a worthwhile task. - Alex

0