0

ターゲットフォルダにシリアル化されたファイルが含まれていない場合にのみ、シリアル化されたファイルをソースフォルダからターゲットフォルダにコピーするプログラムを作成する必要があるため、最初の条件は、コピーするファイルがターゲットフォルダにすでに存在するかどうかを確認することです。存在する場合はコピーする必要はありませんが、存在しない場合はコピーするため、ファイルが存在するかどうかのこのチェックは毎秒実行する必要があります

ソースフォルダはC:\ ter \です。ターゲットフォルダはC:\bvg\です。

転送されるファイルはgfr.serです

私はこの以下のプログラムを思いついたが、それでもチェックが実装されていないこのチェックを実装する方法を教えてください。

クラスScheduledTaskはTimerTaskを拡張します{

public void run() {
    InputStream inStream = null;
    OutputStream outStream = null;

    try {
        File source = new File("C:\\ter\\");
        File target = new File("C:\\avd\\bvg\\");

        // 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();
    }
}

}

    the above approach is not working         
4

1 に答える 1

3

このようなクラスのexistsメソッドを使用できます。java.io.File

 public void run() {
        InputStream inStream = null;
        OutputStream outStream = null;
        try {
            File source = new File("C:\\ter\\gfr.ser");
            File target = new File(" C:\\bvg\\gfr.ser");
            if (target.exists()){   // Already exists. do not copy
                 return;
            }
            inStream = new FileInputStream(source);
            outStream = new FileOutputStream(target);
            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();
        }
    }
于 2013-02-03T07:05:30.313 に答える