1. 程式人生 > >java 讀寫文件常用方法

java 讀寫文件常用方法

tools int big cti readline out 隨機 i++ utf

package study.bigdata;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Random; import java.util.UUID; /** * <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> </dependencies>
*/ public class App { /** * 一行一行地讀取文件的例子 * * @throws IOException */ @Test public void fileUtilsreadLinesTest() throws IOException { List<String> lines = FileUtils.readLines(new File("D:\\___WORK\\workSpaceHome\\temp\\study3\\toolSet\\src\\main\\java\\resource\\test.dat"), "UTF-8"); System.out.println(lines); }
/** * 將String寫入文件的方法 * * @throws IOException */ @Test public void fileUtilswriteStringToFile() throws IOException { File file = new File("D:\\test\\toolSet\\fileutils\\output\\out2.dat"); StringBuffer sb = new StringBuffer(); 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"); } /** * 生成隨機數字和字符串 */ @Test public void randomStringUtilsrandom() { 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)); } @Test public void writeFile() throws IOException { File file = new File("D:\\test\\toolSet\\fileutils\\output\\out3.dat"); StringBuffer sb = new StringBuffer(); 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的文件 * @throws IOException */ @Test public void test1g() throws IOException { FileWriter writer = new FileWriter("D:\\test\\toolSet\\fileutils\\output\\out4.dat"); BufferedWriter buffer = new BufferedWriter(writer); StringBuilder sb = new StringBuilder(); 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(); } }

java 讀寫文件常用方法