复制代码代码如下:
Paket com.robin;
java.io.File importieren;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
öffentliche Klasse FileCopy {
// private static String rootSourcePath = "D:/temp/test1/";
privater statischer String rootSourcePath;
privater statischer String rootTargetPath;
privater statischer String logFilePath;
privater statischer String messageStr;
public static void main(String args[]) {
// prüfen();
LoadConfig();
writeLogLn("Start-------------------------------------");
copyDirectory(rootSourcePath, rootTargetPath);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
Datum sourceFileDate = new Date();
String sourceFileDateStr = sdf.format(sourceFileDate);
writeLogLn("Endzeit:" + sourceFileDateStr + " --------------------------------------" );
writeLogLn("");
}
private static void copyFile(String sourceFileStr, String targetFileStr) {
Datei sourceFile = new File(sourceFileStr);
Datei targetFile = new File(targetFileStr);
// Änderungszeit der Quelldatei abrufen
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
long sourceFileModifiedTime = sourceFile.lastModified();
Datum sourceFileDate = neues Datum(sourceFileModifiedTime);
String sourceFileDateStr = sdf.format(sourceFileDate);
// Wenn Zieldatei vorhanden ist, vergleiche die Änderungszeit
if (targetFile.exists()) {
long targetFileModifiedTime = targetFile.lastModified();
Datum targetFileDate = neues Datum(targetFileModifiedTime);
String targetFileDateStr = sdf.format(targetFileDate);
if (targetFileModifiedTime >= sourceFileModifiedTime) {
messageStr = „Ignorieren! SourceFileModifyTime:“ + sourceFileDateStr
+ " TargetFileModifyTime:" + targetFileDateStr;
} anders {
// nioTransferCopy(sourceFile, targetFile);
systemCopy(sourceFileStr, targetFileStr);
messageStr = "Covere! SourceFileModifyTime: " + sourceFileDateStr
+ "TargetFileModifyTime: " + targetFileDateStr;
}
} anders {
// nioTransferCopy(sourceFile, targetFile);
systemCopy(sourceFileStr, targetFileStr);
messageStr = "Create! SourceFileModifyTime: " + sourceFileDateStr;
}
messageStr += " | SouceFile: " + sourceFileStr + " | Zieldatei: "
+ targetFileStr;
Outputln(messageStr);
writeLogLn(messageStr);
}
private static void copyDirectory(String sourceDirectoryPath,
String targetDirectoryPath) {
// Verzeichnis erstellen, falls es nicht existiert
Datei targetDirectory = new File(targetDirectoryPath);
if (!targetDirectory.exists()) {
targetDirectory.mkdir();
messageStr = "Verzeichnis erstellen: " + targetDirectoryPath;
Outputln(messageStr);
writeLogLn(messageStr);
}
// alle Dateien oder Verzeichnisse im Quellverzeichnis abrufen
File sourceDirectory = new File(sourceDirectoryPath);
File[] files = sourceDirectory.listFiles();
// Dateien kopieren
for (int i = 0; i < files.length; i++) {
String filename = files[i].getName();
String targetFileStr = targetDirectoryPath + Dateiname;
String sourceFileStr = files[i].toString();
if (files[i].isFile()) {
copyFile(sourceFileStr, targetFileStr);
}
if (files[i].isDirectory()) {
targetFileStr = targetFileStr + "/";
copyDirectory(sourceFileStr, targetFileStr);
}
}
}
// private static void nioTransferCopy(Dateiquelle, Dateiziel)
// wirft eine IOException {
// FileChannel in = null;
// FileChannel out = null;
// FileInputStream inStream = null;
// FileOutputStream outStream = null;
// versuchen {
// inStream = new FileInputStream(source);
// outStream = new FileOutputStream(target);
// in = inStream.getChannel();
// out = outStream.getChannel();
// in.transferTo(0, in.size(), out);
// } Catch (IOException e) {
// e.printStackTrace();
// } Endlich {
// inStream.close();
// in.close();
// outStream.close();
// out.close();
// }
// }
private static void systemCopy(String sourceFileStr, String targetFileStr) {
Laufzeit runtime = Runtime.getRuntime();
sourceFileStr = sourceFileStr.replace("/", "//");
targetFileStr = targetFileStr.replace("/", "//");
versuchen {
String copyCmd = "cmd /c copy /y /"" + sourceFileStr + "/" /""
+ targetFileStr + "/"";
runtime.exec(copyCmd);
} Catch (IOException e) {
e.printStackTrace();
}
}
private static void loadConfig() {
versuchen {
FileInputStream inputFile = new FileInputStream(
"config.properties");
Eigenschaften p = neue Eigenschaften();
p.load(inputFile);
rootSourcePath = p.getProperty("rootSourcePath");
rootTargetPath = p.getProperty("rootTargetPath");
logFilePath = p.getProperty("logFilePath");
rootSourcePath = new String(rootSourcePath.getBytes("8859_1"), "GBK");
rootTargetPath = new String(rootTargetPath.getBytes("8859_1"), "GBK");
logFilePath = new String(logFilePath.getBytes("8859_1"), "GBK");
Outputln("SourcePath: " + rootSourcePath);
Outputln("TargetPath: " + rootTargetPath);
Outputln("logFilePath: " + logFilePath);
writeLogLn("SourcePath: " + rootSourcePath);
writeLogLn("TargetPath: " + rootTargetPath);
} Catch (IOException e1) {
e1.printStackTrace();
}
}
private static void outputln(String message) {
System.out.println(message);
}
public static void appendWrite(String content) {
versuchen {
FileWriterwriter = new FileWriter(logFilePath, true);
Writer.write(content);
Writer.close();
} Catch (IOException e) {
e.printStackTrace();
}
}
private static void writeLogLn(String message) {
appendWrite(message+"/r/n");
}
}