复制代码代码如下:
paket com.robin;
impor java.io.File;
impor java.io.FileInputStream;
impor java.io.FileWriter;
impor java.io.IOException;
impor java.teks.SimpleDateFormat;
import java.util.Date;
impor java.util.Properti;
Salinan File kelas publik {
// String statis pribadi rootSourcePath = "D:/temp/test1/";
string statis pribadi rootSourcePath;
string statis pribadi rootTargetPath;
logFilePath String statis pribadi;
pesanStr statis pribadiStr;
public static void main(String args[]) {
// tes();
loadConfig();
writeLogLn("Mulai---------------------------------------");
copyDirectory(rootSourcePath, rootTargetPath);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
Tanggal sourceFileDate = Tanggal baru();
String sourceFileDateStr = sdf.format(sourceFileDate);
writeLogLn("Waktu Berakhir:" + sourceFileDateStr + " --------------------------------------" );
tulisLogLn("");
}
copyFile kekosongan statis pribadi(String sourceFileStr, String targetFileStr) {
File sourceFile = File baru(sourceFileStr);
File targetFile = File baru(targetFileStr);
// dapatkan waktu modifikasi file sumber
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
panjang sourceFileModifiedTime = sourceFile.lastModified();
Tanggal sourceFileDate = Tanggal baru(sourceFileModifiedTime);
String sourceFileDateStr = sdf.format(sourceFileDate);
// jika file target ada, bandingkan waktu modifikasi
if (targetFile.ada()) {
panjang targetFileModifiedTime = targetFile.lastModified();
Tanggal targetFileDate = Tanggal baru(targetFileModifiedTime);
String targetFileDateStr = sdf.format(targetFileDate);
jika (targetFileModifiedTime >= sourceFileModifiedTime) {
messageStr = "Abaikan! SourceFileModifyTime:" + sourceFileDateStr
+ " TargetFileModifyTime:" + targetFileDateStr;
} kalau tidak {
// nioTransferCopy(file sumber, file target);
systemCopy(sourceFileStr, targetFileStr);
messageStr = "Sampul! SourceFileModifyTime: " + sourceFileDateStr
+ "TargetFileModifyTime: " + targetFileDateStr;
}
} kalau tidak {
// nioTransferCopy(file sumber, file target);
systemCopy(sourceFileStr, targetFileStr);
messageStr = "Buat! SourceFileModifyTime: " + sourceFileDateStr;
}
messageStr += " | File Sumber: " + sourceFileStr + " | File Target: "
+ targetFileStr;
keluaranln(messageStr);
writeLogLn(messageStr);
}
copyDirectory void statis pribadi (String sourceDirectoryPath,
String targetDirectoryPath) {
// buat direktori jika belum ada
File targetDirectory = File baru(targetDirectoryPath);
if (!targetDirectory.exists()) {
targetDirectory.mkdir();
messageStr = "Buat Direktori: " + targetDirectoryPath;
keluaranln(messageStr);
writeLogLn(messageStr);
}
// dapatkan semua file atau direktori di direktori sumber
File sourceDirectory = File baru (sourceDirectoryPath);
File[] file = sourceDirectory.listFiles();
// melintasi file salinan
for (int i = 0; i < file.panjang; i++) {
String nama file = file[i].getName();
String targetFileStr = targetDirectoryPath + nama file;
String sourceFileStr = file[i].toString();
jika (file[i].isFile()) {
copyFile(sourceFileStr, targetFileStr);
}
jika (file[i].isDirectory()) {
targetFileStr = targetFileStr + "/";
copyDirectory(sourceFileStr, targetFileStr);
}
}
}
// private static void nioTransferCopy (Sumber file, Target file)
// melempar IOException {
// Saluran File di = null;
// FileChannel keluar = null;
// FileInputStream dalamStream = null;
// FileOutputStream aliran keluar = null;
// mencoba {
// inStream = FileInputStream baru(sumber);
// outStream = FileOutputStream baru(target);
// di = diStream.getChannel();
// keluar = keluarStream.getChannel();
// masuk.transferTo(0, masuk.ukuran(), keluar);
// } tangkapan (IOException e) {
// e.printStackTrace();
// } Akhirnya {
// dalamStream.close();
// masuk.tutup();
// keluarStream.close();
// keluar.tutup();
// }
// }
private static void systemCopy(String sourceFileStr, String targetFileStr) {
Waktu proses = Waktu proses.getRuntime();
sourceFileStr = sourceFileStr.replace("/", "//");
targetFileStr = targetFileStr.replace("/", "//");
mencoba {
String copyCmd = "cmd /c salin /y /"" + sourceFileStr + "/" /""
+ targetFileStr + "/"";
runtime.exec(copyCmd);
} tangkapan (IOException e) {
e.printStackTrace();
}
}
private static void loadConfig() {
mencoba {
FileInputStream inputFile = FileInputStream baru(
"config.properties");
Properti p = Properti baru();
p.beban(masukanFile);
rootSourcePath = p.getProperty("rootSourcePath");
rootTargetPath = p.getProperty("rootTargetPath");
logFilePath = p.getProperty("logFilePath");
rootSourcePath = String baru(rootSourcePath.getBytes("8859_1"), "GBK");
rootTargetPath = String baru(rootTargetPath.getBytes("8859_1"), "GBK");
logFilePath = String baru(logFilePath.getBytes("8859_1"), "GBK");
outputln("Jalur Sumber: " + rootSourcePath);
outputln("Jalur Target: " + rootTargetPath);
outputln("logFilePath: " + logFilePath);
writeLogLn("Jalur Sumber: " + rootSourcePath);
writeLogLn("Jalur Target: " + rootTargetPath);
} tangkapan (IOException e1) {
e1.printStackTrace();
}
}
private static void outputln(Pesan string) {
System.out.println(pesan);
}
public static void appendWrite(Konten string) {
mencoba {
Penulis FileWriter = FileWriter baru(logFilePath, benar);
penulis.tulis(konten);
penulis.close();
} tangkapan (IOException e) {
e.printStackTrace();
}
}
private static void writeLogLn(Pesan string) {
appendWrite(pesan+"/r/n");
}
}