亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java制作復(fù)制文件工具代碼分享

 更新時(shí)間:2014年01月24日 14:02:54   作者:  
如果目標(biāo)位置沒(méi)有同名文件,則直接拷貝過(guò)去;如果目標(biāo)位置已有同名文件,則比對(duì)文件的最后修改日期,來(lái)進(jìn)行覆蓋或者忽略。程序會(huì)在可以在復(fù)制過(guò)程中自動(dòng)創(chuàng)建目錄,并生成log文件,創(chuàng)建了哪些目錄、文件,覆蓋了哪些文件、跳過(guò)了哪些文件,文件的時(shí)間、位置等信息都一目了然

復(fù)制代碼 代碼如下:

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

}

相關(guān)文章

  • 淺談springMVC接收前端json數(shù)據(jù)的總結(jié)

    淺談springMVC接收前端json數(shù)據(jù)的總結(jié)

    下面小編就為大家分享一篇淺談springMVC接收前端json數(shù)據(jù)的總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • IDEA插件推薦之Maven-Helper的教程圖解

    IDEA插件推薦之Maven-Helper的教程圖解

    這篇文章主要介紹了IDEA插件推薦之Maven-Helper的相關(guān)知識(shí),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考
    2020-07-07
  • Java正則表達(dá)式之分組和替換方式

    Java正則表達(dá)式之分組和替換方式

    這篇文章主要介紹了Java正則表達(dá)式之分組和替換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • 使用RestTemplate調(diào)用RESTful?API的代碼示例

    使用RestTemplate調(diào)用RESTful?API的代碼示例

    在開(kāi)發(fā)?Web?應(yīng)用程序時(shí),調(diào)用?RESTful?API?是一個(gè)常見(jiàn)的任務(wù),本文將介紹如何使用?RestTemplate?調(diào)用?RESTful?API,并提供示例代碼,感興趣的同學(xué)可以跟著小編一起來(lái)看看
    2023-06-06
  • spring boot打jar包發(fā)布的方法

    spring boot打jar包發(fā)布的方法

    這篇文章主要介紹了spring boot打jar包發(fā)布的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • java利用delayedQueue實(shí)現(xiàn)本地的延遲隊(duì)列

    java利用delayedQueue實(shí)現(xiàn)本地的延遲隊(duì)列

    這篇文章主要給大家介紹了java利用delayedQueue實(shí)現(xiàn)本地的延遲隊(duì)列的相關(guān)資料,文中介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-04-04
  • Java8中對(duì)泛型目標(biāo)類型推斷方法的改進(jìn)

    Java8中對(duì)泛型目標(biāo)類型推斷方法的改進(jìn)

    這篇文章主要介紹了Java8中對(duì)泛型目標(biāo)類型推斷方法的改進(jìn),需要的朋友可以參考下
    2014-06-06
  • Java web網(wǎng)站訪問(wèn)量的統(tǒng)計(jì)

    Java web網(wǎng)站訪問(wèn)量的統(tǒng)計(jì)

    這篇文章主要為大家詳細(xì)介紹了Java web網(wǎng)站訪問(wèn)量的統(tǒng)計(jì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Eclipse搭建spring開(kāi)發(fā)環(huán)境圖文教程(推薦)

    Eclipse搭建spring開(kāi)發(fā)環(huán)境圖文教程(推薦)

    下面小編就為大家?guī)?lái)一篇Eclipse搭建spring開(kāi)發(fā)環(huán)境圖文教程(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-07-07
  • java實(shí)現(xiàn)分布式項(xiàng)目搭建的方法

    java實(shí)現(xiàn)分布式項(xiàng)目搭建的方法

    這篇文章主要介紹了java實(shí)現(xiàn)分布式項(xiàng)目搭建的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04

最新評(píng)論