功能
将wallpaper目录下面的视频转移到自己所需的目录下面
使用
三个输入:
第一个输入的是wallpaper存放壁纸的地址
其中地址就为
E:\steam\Steam\steamapps\workshop\content\431960
如果不知道也可以右键壁纸查看
第二个输入的是下载的时间,
意思就是从哪个时间开始的壁纸,就比如2022-12-4-19-0,就会找12月4号下午七点以后下载的壁纸
第三个输入的是存放地址
这个就填要存放在哪里的地址。可以是电脑的,也可以把手机连上直接下载
所需依赖:
1 2 3 4 5
| <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency>
|
更新了方法
使用了线程池,线程池的数量等于cpu核处理速度最快
使用了Files.copy方法,使用该方法在复制小文件的过程中速度比BufferedInputStream更快
使用了synchronizedList来统计文件的个数,因为此集合是线程安全的。
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
| package com.company;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject;
import java.io.*; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.*;
public class WallPaper {
private static final ExecutorService executor = Executors.newFixedThreadPool(6);
public static void main(String[] args) throws ParseException, InterruptedException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm"); List<String> list = Collections.synchronizedList(new ArrayList<String>());
Scanner scanner = new Scanner(System.in); System.out.println("请输入wallpaper下载的地址"); String str = scanner.next(); File sfile = new File(str); System.out.println("请输入时间(格式为:年-月-日-小时-分钟)"); String time = scanner.next(); Date date = sdf.parse(time); System.out.println("请输入需要存放的地址"); String str2 = scanner.next(); File tfile = new File(str2); if (!tfile.exists() && !tfile.isDirectory()) { tfile = new File(tfile.getParentFile(), "wallpaper下载的视频"); System.out.println(tfile.getAbsolutePath()); tfile.mkdirs(); }
if (!sfile.exists() && !sfile.isDirectory()) { System.out.println("地址不对哦"); return; } if (sfile.isDirectory()) { File[] fs = sfile.listFiles(); File finalTfile = tfile; int tmp = 0;
assert fs != null;
for (File x : fs) { Date date1 = new Date(x.lastModified()); if (date1.compareTo(date) > 0) { boolean flag = false; String needFilename = x.getAbsolutePath() + "\\project.json"; if (new File(needFilename).exists()) { flag = isNeedFile(needFilename); } if (flag) { tmp++; } } }
CountDownLatch cdl = new CountDownLatch(tmp);
Date startDate = new Date(); for (File x : fs) { executor.submit( () -> { Date date1 = new Date(x.lastModified()); if (date1.compareTo(date) > 0) { boolean flag = false; String needFilename = x.getAbsolutePath() + "\\project.json"; if (new File(needFilename).exists()) { flag = isNeedFile(needFilename); } if (flag) { String filename = x.getAbsolutePath() + "\\" + Filename(needFilename); File tf = new File(finalTfile.getAbsoluteFile() + "\\" + Filename(needFilename)); try { copyfile(new File(filename), tf); synchronized (list){ list.add("成功"); } System.out.println("将 " + Filename(needFilename) + " 复制到 " + finalTfile.getAbsolutePath() + " 中"); } catch (IOException e) { e.printStackTrace(); System.out.println(Filename(needFilename) + "复制失败"); }
cdl.countDown(); } } }); }
cdl.await(); executor.shutdown(); System.out.println("复制完成,共" + list.size() + "个文件"); Date endDate = new Date(); System.out.println(endDate.getTime()-startDate.getTime()); } else { System.out.println("地址不对哦"); }
}
public synchronized static void copyfile(File fs, File ft) throws IOException {
if (ft.exists()) { System.out.println(ft.getName() + "重复...." + "正在删除"); ft.delete(); } Files.copy(fs.toPath(),ft.toPath(),StandardCopyOption.REPLACE_EXISTING);
}
public static boolean isNeedFile(String filename) {
String s = readJsonFile(filename); JSONObject jobj = JSON.parseObject(s); if (jobj.get("contentrating") != null && !jobj.get("contentrating").equals("")) return jobj.get("contentrating").toString().equals("Mature") && (jobj.get("type").toString().equals("Video") || jobj.get("type").toString().equals("video")); return false; }
public static String Filename(String filename) {
String s = readJsonFile(filename); JSONObject jobj = JSON.parseObject(s); return jobj.get("file").toString(); }
public static String readJsonFile(String fileName) { String jsonStr = ""; try { File jsonFile = new File(fileName); FileReader fileReader = new FileReader(jsonFile); Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8"); int ch = 0; StringBuffer sb = new StringBuffer(); while ((ch = reader.read()) != -1) { sb.append((char) ch); } fileReader.close(); reader.close(); jsonStr = sb.toString(); return jsonStr; } catch (IOException e) { e.printStackTrace(); return null; } }
}
|