欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java制作复制文件工具代码分享

程序员文章站 2024-02-23 17:38:52
复制代码 代码如下:package com.robin; import java.io.file;import java.io.fileinputstream;impor...

复制代码 代码如下:

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 was 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 {
 // filechannel 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");
 }

}