packagestudy.bigdata;importorg.apache.commons.io.FileUtils;importorg.apache.commons.io.IOUtils;importorg.apache.commons.lang3.RandomStringUtils;importorg.junit.Test;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileWriter;importjava.io.IOException;importjava.util.List;importjava.util.Random;importjava.util.UUID;/***
junit
junit
commons-io
commons-io
org.apache.commons
commons-lang3
*/
public classApp {/*** 一行一行地读取文件的例子
*
*@throwsIOException*/@Testpublic void fileUtilsreadLinesTest() throwsIOException {
List lines = FileUtils.readLines(new File("D:\\___WORK\\workSpaceHome\\temp\\study3\\toolSet\\src\\main\\java\\resource\\test.dat"), "UTF-8");
System.out.println(lines);
}/*** 将String写入文件的方法
*
*@throwsIOException*/@Testpublic void fileUtilswriteStringToFile() throwsIOException {
File file= new File("D:\\test\\toolSet\\fileutils\\output\\out2.dat");
StringBuffer sb= newStringBuffer();for (int i = 0; i < 3000000; i++) {
sb.append(System.currentTimeMillis()).append(" ").append(UUID.randomUUID()).append("\n");
}
org.apache.commons.io.FileUtils.writeStringToFile(file, sb.toString(),"UTF-8");
}/*** 生成随机数字和字符串*/@Testpublic voidrandomStringUtilsrandom() {
System.out.println(RandomStringUtils.randomAlphabetic(4));
System.out.println(RandomStringUtils.random(5));//产生5位长度的随机字符串,有乱码//使用指定的字符生成5位长度的随机字符串
System.out.println(RandomStringUtils.random(5, new char[]{'a', 'b', 'c', 'd', 'e', 'f'}));//生成指定长度的字母和数字的随机组合字符串
System.out.println(RandomStringUtils.randomAlphanumeric(5));
System.out.println(RandomStringUtils.randomAlphabetic(5));//生成随机数字字符串
System.out.println(RandomStringUtils.randomNumeric(5));
}
@Testpublic void writeFile() throwsIOException {
File file= new File("D:\\test\\toolSet\\fileutils\\output\\out3.dat");
StringBuffer sb= newStringBuffer();for (int i = 0; i < 2000; i++) {
sb.append(RandomStringUtils.randomAlphabetic(5)).append(" ")
.append(RandomStringUtils.randomAlphabetic(5)).append(" ")
.append(RandomStringUtils.randomAlphabetic(4)).append(" ")
.append(RandomStringUtils.randomAlphabetic(6)).append("\n ");
}
FileUtils.writeStringToFile(file, sb.toString(),"UTF-8");
}/*** 写一个1g的文件
*@throwsIOException*/@Testpublic void test1g() throwsIOException {
FileWriter writer= new FileWriter("D:\\test\\toolSet\\fileutils\\output\\out4.dat");
BufferedWriter buffer= newBufferedWriter(writer);
StringBuilder sb= newStringBuilder();for (int j = 0; j < 1024; j++) {
sb.append("1");
}long start =System.currentTimeMillis();int max = 1 * 1024 * 1024;//1G
for (int i = 0; i < max; i++) {
buffer.write(sb.toString());
}long end =System.currentTimeMillis();
System.out.println(end-start);
buffer.flush();
buffer.close();
writer.close();
}
}