配置文件
#{source,target}
#{源目录,目标目录}
#可以指定多个源,每行一个配置
{ E:\a,D:\b}
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
public class FileTest {
//同步文件夹
public static void fileSynchronization(String sourcePath, String targetPath) throws IOException {
File sourceDir = new File(sourcePath);
File targetDir = new File(targetPath);
List<File> sourceDirList = new ArrayList<>();//待遍历的文件夹
sourceDirList.add(sourceDir);
for (int i = 0; i < sourceDirList.size(); i++) {
//遍历开始
File soure = sourceDirList.get(i);
if (soure.getName().equals("#recycle")) continue;
//判断是否存在对应的目标文件夹
File target = new File(soure.toString().replace(sourcePath, targetPath));
if (target.isFile()) target.delete();
if (!target.exists()) {
target.mkdirs();
System.out.println("创建文件夹" + target.toString());
}
//获取目录下所有一级文件/文件夹
File[] files = soure.listFiles();
//如果文件夹不存在或者是空文件夹,跳过当次
if (files == null || files.length == 0) continue;
for (int j = 0; j < files.length; j++) {
//如果是目录,添加到待遍历集合
if (files[j].isDirectory()) {
sourceDirList.add(files[j]);
} else {
//如果是文件,判断目标文件是否存在
File targetFile = new File(files[j].toString().replace(sourcePath, targetPath));
if (!targetFile.exists()) {
//不存在,直接复制文件
System.out.println("复制文件" + files[j].toString() + "到" + targetFile.toString());
copyFile(files[j], targetFile);
} else {
//存在,比较大小
if (files[j].length() != targetFile.length()) {
//大小不一致,更新文件
System.out.println("更新文件" + files[j].toString() + "到" + targetFile.toString());
targetFile.delete();
copyFile(files[j], targetFile);
}
}
}
}
}
List<File> targetDirList = new ArrayList<>();//待遍历的文件夹
targetDirList.add(targetDir);
for (int i = 0; i < targetDirList.size(); i++) {
File[] files = targetDirList.get(i).listFiles();
if (files == null || files.length == 0) continue;
for (int j = 0; j < files.length; j++) {
if (files[j].isDirectory()) {
targetDirList.add(files[j]);
continue;
}
File file = new File(files[j].toString().replace(targetPath, sourcePath));
if (!file.exists()) {
System.out.println("删除文件" + files[j].toString());
files[j].delete();
}
}
}
}
public static void copyFile(File sourceFile, File destinationFile ) {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = new FileInputStream(sourceFile);
outputStream = new FileOutputStream(destinationFile) ;
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
System.out.println("复制"+sourceFile.getName()+"成功!");
} catch (IOException e) {
try {
inputStream.close();
outputStream.close();
}catch (IOException io){
io.printStackTrace();
}
e.printStackTrace();
}
}
public static void runFileSynchronization(String confPath) {
try {
List<String> pathList = new ArrayList<>();
BufferedReader bufferedReader = null;
FileInputStream fileInputStream = null;
InputStreamReader inputStreamReader = null;
try {
fileInputStream = new FileInputStream(confPath);
inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
bufferedReader = new BufferedReader(inputStreamReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
pathList.add(line);
}
} finally {
try {
if (bufferedReader != null) bufferedReader.close();
if (inputStreamReader != null) inputStreamReader.close();
if (fileInputStream != null) fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(pathList);
for (String path : pathList) {
String temp = path.trim();
if (temp.length() == 0) continue;
if (temp.substring(0, 1).equals("#")) continue;
String sourcePath = temp.substring(temp.indexOf("{") + 1, temp.indexOf(",")).trim();
String targetPath = temp.substring(temp.indexOf(",") + 1, temp.indexOf("}")).trim();
System.out.println(sourcePath + " ---> " + targetPath);
fileSynchronization(sourcePath, targetPath);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println("app is run");
String confPath = "app.conf";
if (args.length != 0) {
for (int i = 0; i < args.length; i++) {
if (args[i].trim().equals("-c")) {
confPath = args[i + 1].trim();
}
}
}
System.out.println("配置文件路径" + confPath);
try {
while (1 == 1) {
runFileSynchronization(confPath);
Date date = new Date();
Calendar calendar = Calendar.getInstance();
//假设startDate为要增加时间的Date
calendar.setTime(date);
calendar.add(Calendar.MINUTE, 3 * 60);//这里是增加3*60分钟,即3小时,也可以在第一个形参选择所需单位
date = calendar.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = formatter.format(date);//格式化数据
System.out.println("运行完成,下运行时间" + dateStr);
Thread.sleep(10800000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
下载地址:
文件同步