1. 生成大小相同,内容不同
int differentSize = 128 * 1024 + 1; //128 * 1024 + 1 代表是128k+1 byte[] content = RandomString.make(differentSize).getBytes();
2. 生成大小随机, 内容不同
int differentSize = new Random().nextInt(200 * 1024 * 1024) + 1; //单位:M byte[] content = RandomString.make(differentSize).getBytes();
3. 读指定路径下的文件 大小相同,内容相同
public static String readFilePath = "testdata/readfile/"
public static String randonFileName(String filePath) {
File file = new File(filePath);
String[] filelist = file.list();
// for (int i = 0; i < filelist.length; i++) {
// System.out.println(filelist[i]);
// }
int index = (int) (Math.random() * filelist.length);
String random = filelist[index];
return random;
}
//大小不同,内容相同. 读指定路径下的文件随机
//String readfile = readFilePath + randonFileName(DataUtil.readFilePath);
//相同大小,内容相同. 读指定路径下的单个文件
String readfile = readFilePath + "20m";
System.out.println("文件路径: " + readfile);
bytes = byteMap.computeIfAbsent(readfile, f -> {
try {
return Files.asByteSource(new File(f)).read();
} catch (Exception e) {
throw new RuntimeException(e);
}
});
System.out.println("文件内容: " + new String(bytes));
该文描述了三种不同的文件处理方法:1)生成固定大小、不同内容的字节数组;2)生成大小随机、内容不同的字节数组;3)从指定路径读取文件,获取相同或不同大小但内容相同的文件内容。代码示例展示了如何实现这些功能。

被折叠的 条评论
为什么被折叠?



