Package com.robin;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import java.util.Properties;
public class FileCopy {
private static String Rootsourcepath = "d:/temp/test1/";
private static String Rootsourcepath;
private static String Roottargetpath;
private static String LogFilePath;
private static String messagestr;
public static void Main (String args[]) {
Test ();
Loadconfig ();
Writelogln ("Start--------------------------------------");
CopyDirectory (Rootsourcepath, Roottargetpath);
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd_hh:mm:ss");
Date sourcefiledate = new Date ();
String sourcefiledatestr = Sdf.format (sourcefiledate);
Writelogln ("End time:" + sourcefiledatestr + "--------------------------------------");
Writelogln ("");
}
private static void CopyFile (String sourcefilestr, String targetfilestr) {
File SourceFile = new file (SOURCEFILESTR);
File TargetFile = new file (TARGETFILESTR);
Get source file Modify time
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd_hh:mm:ss");
Long sourcefilemodifiedtime = sourcefile.lastmodified ();
Date sourcefiledate = new Date (sourcefilemodifiedtime);
String sourcefiledatestr = Sdf.format (sourcefiledate);
If target file exist, compare modify time
if (targetfile.exists ()) {
Long targetfilemodifiedtime = targetfile.lastmodified ();
Date targetfiledate = new Date (targetfilemodifiedtime);
String targetfiledatestr = Sdf.format (targetfiledate);
if (targetfilemodifiedtime >= sourcefilemodifiedtime) {
Messagestr = "ignore! Sourcefilemodifytime: "+ sourcefiledatestr
+ "Targetfilemodifytime:" + targetfiledatestr;
} else {
Niotransfercopy (SourceFile, targetfile);
Systemcopy (Sourcefilestr, TARGETFILESTR);
Messagestr = "covere! Sourcefilemodifytime: "+ sourcefiledatestr
+ "Targetfilemodifytime:" + targetfiledatestr;
}
} else {
Niotransfercopy (SourceFile, targetfile);
Systemcopy (Sourcefilestr, TARGETFILESTR);
Messagestr = "create! Sourcefilemodifytime: "+ sourcefiledatestr;
}
Messagestr + = "| Soucefile: "+ sourcefilestr +" | Target File: "
+ targetfilestr;
Outputln (MESSAGESTR);
Writelogln (MESSAGESTR);
}
private static void CopyDirectory (String sourcedirectorypath,
String Targetdirectorypath) {
Create directory If it is not exist
File TargetDirectory = new file (Targetdirectorypath);
if (!targetdirectory.exists ()) {
Targetdirectory.mkdir ();
Messagestr = "Make Directory:" + targetdirectorypath;
Outputln (MESSAGESTR);
Writelogln (MESSAGESTR);
}
Get all files or directories on source directory
File Sourcedirectory = new file (Sourcedirectorypath);
file[] files = sourcedirectory.listfiles ();
Traverse Copy Files
for (int i = 0; i < files.length; i++) {
String filename = Files[i].getname ();
String targetfilestr = targetdirectorypath + filename;
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 (file source, file target)
//throws IOException {
//Fi Lechannel in = null;
//FileChannel out = null;
//FileInputStream instream = null;
//FileOutputStream outstream = null;
//try {
//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 ();
//} finally {
//instream.close ();
//In.close ();
//outstream.close ();
//out.close ();
//}
//}
private static void Systemcopy (String sourcefilestr, String targetfilestr) {
Runtime Runtime = Runtime.getruntime ();
Sourcefilestr = Sourcefilestr.replace ("/", "\ \");
Targetfilestr = Targetfilestr.replace ("/", "\ \");
try {
String copycmd = "cmd/c copy/y \" "+ Sourcefilestr +" \ "\" "
+ Targetfilestr + "" ";
Runtime.exec (Copycmd);
catch (IOException e) {
E.printstacktrace ();
}
}
private static void Loadconfig () {
try {
FileInputStream inputfile = new FileInputStream (
"Config.properties");
Properties P = new properties ();
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) {
try {
FileWriter writer = new FileWriter (LogFilePath, true);
Writer.write (content);
Writer.close ();
catch (IOException e) {
E.printstacktrace ();
}
}
private static void Writelogln (String message) {
Appendwrite (message+ "\ r \ n");
}
}