java制作复制文件工具代码分享
发布时间:2022-08-05 05:30:22
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");
}
}


猜你喜欢
- 每次滑动至底端,从数据库中获取10条数据,并加载于ListView中数据库package com.example.listviewbatch
- 初学Android编程,Android SDK中提供的Sample代码自然是最好的学习材料。 
- 前提:windows上安装jdk1.启动jar脚本@echo offSTART "app" javaw -jar app
- 前言昨夜同门云集推杯又换盏,今朝茶凉酒寒豪言成笑谈。半生累,尽徒然,碑文完美有谁看,隐居山水之间誓与浮名散。简介今天给大家带来的是支付宝的月
- spring boot RestTemplate 发送get请求踩坑闲话少说,代码说话RestTemplate 实例手动实例化,这个我基本不
- 一、简介本文主要介绍jmeter在控制台在点击执行之后底层所做的一些主要事情及内容,由于便于断点调试采用GUI方式进行操作二、配置简介为了调
- 本文介绍了android APP登陆页面适配的实现,分享给大家,具体如下:先看效果图。登陆首页效果图原理为RootView增加监听事件,然后
- 前言相信大家都知道Android滚动控件的实现方式有很多, 使用RecyclerView也比较简单. 做了一个简单的年龄滚动控件, 让我们来
- Lambda表达式类似匿名函数,简单地说,它是没有声明的方法,也即没有访问修饰符、返回值声明和方法名。Lambda允许把函数作为一个方法的参
- Springboot 在普通类型注入Service或mapper最近遇到一个难题(大佬可能感觉这太简单了把),对于我这样的小白来说,确实有些
- 实例如下:package com.huad.luck;import java.util.ArrayList;import java.util
- 一、概述一个Process组件提供了在计算机运行进程的访问权限。 进程,在最简单的术语中,是正在运行的应用。提供对本地和远程进程的访问权限并
- 在android提供了一种类型:Parcel。被用作封装数据的容器,封装后的数据可以通过Intent或IPC传递。 除了基本类型以外,只有实
- 一、前沿如果你学习过其他的编程语言,你就会发现 Java 的语法很是哆嗦,可是我们为什么没有放弃 Java 这门编程语言呢?因为 JVM 是
- 这一节我们将探索选择器(selectors)。选择器提供选择执行已经就绪的任务的能力,这使得多元 I/O 成为可能。就像在第一章中描述的那样
- 一、创建数据库 1、新建数据库帮助类 包名——右击—&am
- monaco editor创建//创建和设置值if (!this.monacoEditor) { this.monacoEdit
- 对于数据的访问来说,肯定是在有缓存的情况下运行快一些。对于Hibernate这种与数据库结合紧密的框架来说,在调用数据的时候肯定会有缓存的出
- 前言跳过废话,直接看正文当年入坑Java是因为它的跨平台优势。那时我认为,”编写一次,处处运行。”这听上去多么牛逼,应该是所有语言发展的终极
- RabbitMQ的一些基本组件Producer:消息的生产者Consumer:消息的消费者Broker:MQ服务器,管理队列、消息Messa