import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
/**
* @author 作者 豪哥
* @author 微信 xiaomao0055
* @author qq 14496453
* @version 日期 :2021年10月22日 下午11:06:02
*/
public class T {
public static String backups = "D:\\cocos_backups\\";
public static void main(String[] args) throws NumberFormatException, IOException, InterruptedException {
System.out.println("请输入,所有项目的文件夹路径:");
Scanner sc = new Scanner(System.in);
// String input_path = sc.next();
String input_path = "D:\\cocos_work_space";
List<File> fileList = getAllFolder(input_path);
int index = 0;
for (File file : fileList) {
System.out.println("序号" + index + ":" + file.getPath());
index++;
}
System.out.println("请输入备份的文件夹序号:");
String num = sc.next();
String fileName = fileList.get(Integer.parseInt(num)).getName();
System.out.println("请输入备份的间隔___分钟:");
String fen = sc.next();
while (true) {
System.out.println("开始备份");
// 检测我从下标几开始
String date = formatDate(new Date(), "yyyyMMdd");
String newPath = getIndex(backups, fileName, date);
// 新的路径文件全复制到这里。
System.out.println(newPath);
makeDirs(newPath);
// 遍历这个路径下面的所的文件,包括文件夹
// copy(fileList.get(Integer.parseInt(num)),filedFile);
// copyFolder(fileList.get(Integer.parseInt(num)).getPath(),"D:\\cocos_backups");
ergodicAllFiles(fileList.get(Integer.parseInt(num)), fileList.get(Integer.parseInt(num)).getPath(),
newPath);
System.out.println("备份完成" + formatDate(new Date(), "HH:mm") + "__" + newPath);
// 睡
Thread.sleep(Integer.parseInt(fen) * 6 * 10 * 1000);
}
}
/**
* E:\CocosCreator游戏资源\000我的源码
*
* @author 作者微信 xiaomao0055 backups 检测的路径 "D:\\cocos_backups\\"; fileName
* 检测的文件夹名子 JigsawPuzzle date 当前日期20211023 返回一个路径 :
* D:\cocos_backups\20211023__JigsawPuzzle__1
*/
private static String getIndex(String backups, String fileName, String date) {
List<File> fileList = getAllFolder(backups);
int index = 0;
if (fileList == null || fileList.size() == 0) {
return backups + date + "__" + fileName + "__" + (index + 1);
}
for (File file : fileList) {
String _fileName = file.getName();
String[] temp = _fileName.split("__");
// System.out.println(_fileName+":"+temp.length);
if (temp.length == 3) {
// System.out.println("日期:"+temp[0]+",文件名:"+temp[1]+",下标:"+temp[2]);
if (fileName.equals(temp[1])) {
if (date.equals(temp[0])) {
index = Integer.parseInt(temp[2]);
}
}
}
}
// System.out.println("下标:"+(index+1));
return backups + date + "__" + fileName + "__" + (index + 1);
}
// E:\CocosCreator游戏资源\000我的源码
/**
* 取得路径下面的所有文件夹
*
* @author 作者微信 xiaomao0055
*/
public static List<File> getAllFolder(String path) {
List<File> fileList = new ArrayList<File>();
File file = new File(path);
if (file.exists()) {
File[] files = file.listFiles();
if (null == files || files.length == 0) {
// System.out.println("文件夹是空的!");
return null;
} else {
for (File file2 : files) {
if (file2.isDirectory()) {
fileList.add(file2);
} else {
// System.out.println("文件:" + file2.getAbsolutePath());
}
}
}
} else {
System.out.println("文件不存在!");
}
return fileList;
}
/**
* file:遍历文件夹下面的所有文件,包括文件夹 path1 遍历的文件夹路径 path2 复制的路径
*
* @author 作者微信 xiaomao0055
* @throws IOException
*/
public static void ergodicAllFiles(File file, String path1, String path2) throws IOException {
if (file.exists()) {
// 判断文件是否存在
File[] files = file.listFiles();
if (null == files || files.length == 0) {
// System.out.println("文件夹是空的!");
return;
} else {
for (File file2 : files) {
if (file2.isDirectory()) {
// System.out.println("文件夹:" + file2.getAbsolutePath());
String path = file2.getAbsolutePath();
String newPath = path.replace(path1, path2);
// System.out.println(newPath);
// 创建文件夹
makeDirs(newPath);
// write(file2.getAbsolutePath());
ergodicAllFiles(new File(file2.getAbsolutePath()), path1, path2);
} else {
// System.out.println("文件:" + file2.getAbsolutePath());
String path = file2.getAbsolutePath();
String newPath = path.replace(path1, path2);
// System.out.println(newPath);
copyFileUsingFileChannels(file2, new File(newPath));
// copyFileUsingFileStreams(file2,new File(newPath));
}
}
}
} else {
System.out.println("文件不存在!");
}
}
private static void copyFileUsingFileChannels(File source, File dest) throws IOException {
FileChannel inputChannel = null;
FileChannel outputChannel = null;
try {
inputChannel = new FileInputStream(source).getChannel();
outputChannel = new FileOutputStream(dest).getChannel();
outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
} finally {
inputChannel.close();
outputChannel.close();
}
}
/**
* 创建一个文件夹
* 如果存在 不创建
* 如果不存在 创建
* @param filePath
* @return
*/
public static boolean makeDirs(String filePath) {
File folder = new File(filePath);
return (folder.exists() && folder.isDirectory()) ? true : folder.mkdirs();
}
/**
* yyyyMMdd hhmmssSSS
* 日期对象转字符串
*/
public static String formatDate(Date date,String format){
String result="";
SimpleDateFormat sdf=new SimpleDateFormat(format);
if(date!=null){
result=sdf.format(date);
}
return result;
}
}站长微信:xiaomao0055
站长QQ:14496453