一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - java制作復制文件工具代碼分享

java制作復制文件工具代碼分享

2019-11-04 14:12java技術網 JAVA教程

如果目標位置沒有同名文件,則直接拷貝過去;如果目標位置已有同名文件,則比對文件的最后修改日期,來進行覆蓋或者忽略。程序會在可以在復制過程中自動創建目錄,并生成log文件,創建了哪些目錄、文件,覆蓋了哪些文件

代碼如下:


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");
 }

}

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 美女的让男人桶爽免费看 | 国产成人久久久精品一区二区三区 | 免费在线看a | 武侠艳妇屈辱的张开双腿 | 兽操人 | 91制片厂制作传媒网站破解 | 婚前试爱全集免费观看 | 欧美成人免费tv在线播放 | voyeur 中国女厕 亚洲女厕 | 国产在线精品香蕉综合网一区 | 亚洲 欧美 国产 综合 播放 | 日本国产成人精品视频 | 女色在线观看免费视频 | 国产精品免费网站 | 国产乱子伦在线观看不卡 | 精品国产成人高清在线 | 久久天天躁狠狠躁夜夜躁 | 无人区在线观看免费观看 | 我的美女奴隶 | 日本中出视频 | 亚洲男人天堂网址 | 亚洲成年www | 欧美另类z0zxi | 无码乱人伦一区二区亚洲 | xxxx18日本视频xxxxx | 青青青国产精品国产精品久久久久 | 美女被无套进入 | 好男人免费高清在线观看2019 | 欧美伊人影院 | 九九九九在线视频播放 | 描写细腻的高h肉 | 成人免费视频在 | 性印度freehd| 国产欧美日韩图片一区二区 | 男人天堂视频网 | 2022国产麻豆剧传媒剧情 | 欧美va在线| 欧美成人v视频免费看 | 成人嗯啊视频在线观看 | 3d动漫美女物被遭强视频 | 国产一级在线免费观看 |