时间:2021-05-19
复制代码 代码如下:
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");
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
main.xml文件:复制代码代码如下:listview_item.xml文件:复制代码代码如下:MainActivity.java复制代码代码如下:packa
工具类代码,取得当前项目中所有java文件总行数,代码行数,注释行数,空白行数复制代码代码如下:importjava.io.BufferedReader;imp
本文实例为大家分享了java将一个目录下的所有文件复制n次的具体代码,供大家参考,具体内容如下1.文件复制示意图2.java程序(1).调用finalstati
复制代码代码如下:/**Java文件操作获取文件扩展名**/publicstaticStringgetExtensionName(Stringfilename)
本文实例讲述了Java输入输出流。分享给大家供大家参考,具体如下:字节输出流,输出到文件中(写)OutputStream抽象类选好文件复制代码代码如下:File