java 写文件 util_java 读写文件常用方法

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();

}

}

package com.hexiang.utils; import java.io.*; /** * FileUtil. Simple file operation class. * * @author BeanSoft * */ public class FileUtil { /** * The buffer. */ protected static byte buf[] = new byte[1024]; /** * Read content from local file. FIXME How to judge UTF-8 and GBK, the * correct code should be: FileReader fr = new FileReader(new * InputStreamReader(fileName, "ENCODING")); Might let the user select the * encoding would be a better idea. While reading UTF-8 files, the content * is bad when saved out. * * @param fileName - * local file name to read * @return * @throws Exception */ public static String readFileAsString(String fileName) throws Exception { String content = new String(readFileBinary(fileName)); return content; } /** * 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(String fileName, String encoding) throws Exception { String content = new String(readFileBinary(fileName), encoding); return content; } /** * 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(InputStream in) throws Exception { String content = new String(readFileBinary(in)); return content; } /** * Read content from local file to binary byte array. * * @param fileName - * local file name to read * @return * @throws Exception */ public static byte[] readFileBinary(String fileName) throws Exception { FileInputStream fin = new FileInputStream(fileName); return readFileBinary(fin); } /** * 从输入流读取数据为二进制字节数组. * @param streamIn * @return * @throws IOException */ public static byte[] readFileBinary(InputStream streamIn) throws IOException { BufferedInputStream in = new BufferedInputStream(streamIn); ByteArrayOutputStream out = new ByteArrayOutputStream(10240); int len; while ((len
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值