最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

  • java中多線程加鎖的四種方式

    java中多線程加鎖的四種方式

    Java中實(shí)現(xiàn)多線程安全的關(guān)鍵是加鎖,主要方式有synchronized關(guān)鍵字、ReentrantLock類、ReadWriteLock接口和Semaphore類,本文就來介紹一下這四種方式,感興趣的可以了解一下
    2024-10-10
  • SpringSecurity實(shí)現(xiàn)自定義用戶認(rèn)證方案

    SpringSecurity實(shí)現(xiàn)自定義用戶認(rèn)證方案

    Spring?Security?實(shí)現(xiàn)自定義用戶認(rèn)證方案可以根據(jù)具體需求和業(yè)務(wù)場(chǎng)景進(jìn)行設(shè)計(jì)和實(shí)施,滿足不同的安全需求和業(yè)務(wù)需求,這種靈活性使得認(rèn)證機(jī)制能夠更好地適應(yīng)各種復(fù)雜的環(huán)境和變化?,本文給大家介紹了SpringSecurity實(shí)現(xiàn)自定義用戶認(rèn)證方案,需要的朋友可以參考下
    2025-01-01
  • Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(49)

    Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(49)

    下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你
    2021-08-08
  • 注入jar包里的對(duì)象,用@autowired的實(shí)例

    注入jar包里的對(duì)象,用@autowired的實(shí)例

    這篇文章主要介紹了注入jar包里的對(duì)象,用@autowired的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • Spring依賴注入DI之三種依賴注入類型詳解

    Spring依賴注入DI之三種依賴注入類型詳解

    這篇文章主要介紹了Spring依賴注入DI之三種依賴注入類型詳解,通過 @Autowired 注解,字段注入的實(shí)現(xiàn)方式非常簡(jiǎn)單而直接,代碼的可讀性也很強(qiáng),事實(shí)上,字段注入是三種注入方式中最常用、也是最容易使用的一種,需要的朋友可以參考下
    2023-09-09
  • SpringBoot使用LomBok的示例代碼

    SpringBoot使用LomBok的示例代碼

    這篇文章主要介紹了SpringBoot使用LomBok的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-07-07
  • java和c/c++ 數(shù)據(jù)類型長(zhǎng)度的比較

    java和c/c++ 數(shù)據(jù)類型長(zhǎng)度的比較

    本篇文章主要是對(duì)java和c/c++ 數(shù)據(jù)類型長(zhǎng)度的進(jìn)行了詳細(xì)的比較。需要的朋友可以過來參考下,希望對(duì)大家有所幫助
    2014-01-01
  • 基于java ssm springboot實(shí)現(xiàn)選課推薦交流平臺(tái)系統(tǒng)

    基于java ssm springboot實(shí)現(xiàn)選課推薦交流平臺(tái)系統(tǒng)

    這篇文章主要介紹了選課推薦交流平臺(tái)系統(tǒng)是基于java ssm springboot來的實(shí)現(xiàn)的,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08
  • java編寫簡(jiǎn)單的E-mail發(fā)送端程序

    java編寫簡(jiǎn)單的E-mail發(fā)送端程序

    這篇文章主要介紹了使用java語言編寫一個(gè)簡(jiǎn)單的E-mail發(fā)送端程序,感興趣的小伙伴們可以參考一下
    2016-02-02
  • Java數(shù)據(jù)結(jié)構(gòu)及算法實(shí)例:樸素字符匹配 Brute Force

    Java數(shù)據(jù)結(jié)構(gòu)及算法實(shí)例:樸素字符匹配 Brute Force

    這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)及算法實(shí)例:樸素字符匹配 Brute Force,本文直接給出實(shí)例代碼,代碼中包含詳細(xì)注釋,需要的朋友可以參考下
    2015-06-06

最新評(píng)論

广德县| 商洛市| 湘潭县| 竹山县| 垣曲县| 浦城县| 新密市| 揭东县| 弥渡县| 卓尼县| 正蓝旗| 武安市| 军事| 泗洪县| 新安县| 东兴市| 天全县| 普安县| 东平县| 武夷山市| 邵阳市| 神农架林区| 海林市| 焉耆| 黄梅县| 高唐县| 清河县| 柯坪县| 淄博市| 宝丰县| 噶尔县| 绵阳市| 吉木萨尔县| 金溪县| 教育| 望江县| 会理县| 当涂县| 涟源市| 八宿县| 承德县|