ソースフォルダーから宛先フォルダーにファイルをコピーするJavaプログラムを開発しました。ソースフォルダーから宛先フォルダーにコピーする10個のシリアル化されたファイルがあります
ただし、ファイルが宛先フォルダーに既に存在する場合、その場合はコピーしてはならないため、基本的には1秒以内に検索が行われ、宛先フォルダーにそれらの10個のシリアル化されたファイルが含まれていることを確認します。そうでない場合は、その場合のみコピーする必要があり、コピーした後、ファイルが存在するかどうかを1秒以内に再度確認する必要があります。これを達成する方法を教えてください
//Create a class extends with TimerTask
class ScheduledTask extends TimerTask {
public void run() {
InputStream inStream = null;
OutputStream outStream = null;
try {
File source = new File("C:\\cache\\");
File target = new File("C:\\Authclient\\cache\\");
// Already exists. do not copy
/*if (target.exists()) {
return;
}*/
File[] files = source.listFiles();
for (File file : files) {
inStream = new FileInputStream(file);
outStream = new FileOutputStream(target + "/" + file.getName());
byte[] buffer = new byte[1024];
int length;
// copy the file content in bytes
while ((length = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.close();
}
System.out.println("File is copied successful!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Copycache {
public static void main(String args[]) throws InterruptedException {
Timer time = new Timer();
ScheduledTask task = new ScheduledTask();
time.schedule(task, new Date(), TimeUnit.SECONDS.toMillis(1));
}
}
上記の実装が存在し、コメントされていますが、正しい儀式が機能していません。アドバイスしてください