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");
}
}
猜你喜欢
- 从现在开始,大家可以跟随着我的脚步来自定义一个属于自己的Spring框架。但是,在学习自定义Spring框架之前,我们得先来回顾一下Spri
- 1、线程的控制很常见,如文件传送到一半时,需要暂停文件传送,或终止文件传送,这实际上就是控制线程的运行。2、线程有创建、可运行、运行中、阻塞
- 泛型中占位符T和?有什么区别?这是一个好问题,有的人可能弄不清楚,所以我们这里简单的演示一下,相信大家一定能弄清楚的!先上两段代码:publ
- ListView在实际实用中,一般都会有下新刷新和上拉加载的动态效果,今天要学的就是如何自定义带下拉刷新的ListView。原理解析:一般将
- 最近项目上用就hibernate+spring data jpa,一开始感觉还不错,但是随着对业务的复杂,要求处理一些复杂的sql,就顺便研
- 项目中经常会使用到一对多的查询场景,但是PageHelper对这种嵌套查询的支持不够,如果是一对多的列表查询,返回的分页结果是不对的参考Gi
- 我就废话不多说了,大家还是直接看代码吧~package cn.nxl2018;class Test{ //十进制常量赋值 &n
- 什么是WebSocket WebSocket是一种在单个TCP连接上进行全双工通信的协议。WebSocket使得客户端和服务器之间
- java 制作验证码并进行验证实例详解在注册、登录的页面上经常会出现验证码,为了防止频繁的注册或登录行为。下面是我用java制作的一个验证码
- idea手动刷新git分支相信很多小伙伴都遇到过这样的问题,在git上新建的分支却在idea的git分支中找不到又不知在哪里刷新,博主最近也
- 在使用 springboot 或者 springcloud 开发的时候,通常为了保证系统的安全性,配置文件中的密码等铭感信息都会进行加密处理
- 大家都知道NPOI组件可以在你本地没有安装office的情况下来 读取,创建excel文件。但是大家一般都是只默认读取一个excel文件的第
- 使用对象初始值设定项初始化对象可以使用对象初始值设定项以声明方式初始化类型对象,而无需显式调用类型的构造函数。下面的示例演示如何将对象初始值
- 前言开发传统Java WEB工程时,我们可以使用JSP页面模板语言,但是在SpringBoot中已经不推荐使用了。SpringBoot支持如
- 使用 replace 函数动态填充字符串String str="Hello {0},我是 {1},今年{2}岁"
- HashTable和HashMap区别第一,继承的父类不同。Hashtable继承自Dictionary类,而HashMap继承自Abstr
- 一、首先编写一个工具类Hello:public class Hello { public static void say(Str
- 分析代码规范,并从代码角度分析存在的风险,并且支持一键更改。具体操作如下图:简单方便,最主要的是好用!补充:下面看下IntelliJ IDE
- AlertDialog可以在当前的界面上显示一个对话框,这个对话框是置顶于所有界面元素之上的,能够屏蔽掉其他控件的交互能力,因此AlertD
- 在C#winform程序开发过程中,我们可能需要定期去设定一些变化的值,但这些值在程序中又要被用来做对比或参照,比如我们设定一个固定值让程序