1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | 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