笔记本电脑出现问题,想着拿到售后服务中心检修,担心删除的文件被恢复窥探,遂编写程序试图将D盘塞满覆盖之前的使用痕迹。感觉挺无聊的。
private static Random random = new Random();
private static List<Character> chars = new ArrayList<>();
// 线程池
private static ExecutorService executorService = Executors.newFixedThreadPool(6);
static {
for (char c = 'A'; c <= 'Z'; c++) {
chars.add(c);
chars.add((char) (c + 32));
}
}
public static void main(String[] args) {
File sourceRoot = new File("E:/娱乐电影/铁齿铜牙纪晓岚第一部");
File[] files = sourceRoot.listFiles();
while (random.nextInt(600) != 168) {
// lambda表达式
executorService.execute(() -> {
File originFile = getFile(files);
file file = generateFile();
try(InputStream in = new FileInputStream(originFile);
OutputStream out = new FileOutputStream(file)) {
byte[] buffer = new byte[in.available()];
in.read(buffer);
out.write(buffer);
} catch (IOException e) {
e.printStackTrace();
}
});
}
executorService.shutdown();
}
private static File generateFile() {
StringBuffer sb = new StringBuffer("D:/");
// 目录层级
int level = randomInt(6);
// 名称长度
int length = randomInt(20);
// 拼接多级目录名,比如 D:/aZw/UoPye/
for (int i = 0; i < level; i++) {
for (int j = 0; j < length; j++) {
sb.append(getChar());
}
sb.append("/");
}
// 创建多级目录
File root = new File(sb.toString());
root.mkdirs();
// 文件名
for (int j = 0; j < length; j++) {
sb.append(getChar());
}
// 创建文件
File file = new File(sb.toString));
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
// 随机获取一个字符
private static char getChar() {
return chars.get(random.nextInt(chars.size()));
}
// 保证目录至少有两级,或者名称至少有两个字符
private static int randomInt(int bound) {
int i;
do {
i = random.nextInt(bound);
} while (i < 2);
return i;
}
// 随机获取一个文件
private static File getFile(File[] files) {
return files[random.nextInt(files.length)];
}
第一次接触lambda表达式,是在3年前阅读刚接触的项目的源码。当时看不懂,于是找度娘,才知道是Java8新特性lambda表达式,照着例子敲了demo。前段时间,在“多线程”专题中,讲师大幅度使用lambda表达式,感觉十分优雅。