guava io package com.liu.io;import java.io.File;import java.io.IOException;import java.util.List;import org.junit.Test;import com.google.common.base.Charsets;import com.google.common.io.Files;public class GuavaIo {/** * 读取文件,list的
package com.liu.io; import java.io.File; import java.io.IOException; import java.util.List; import org.junit.Test; import com.google.common.base.Charsets; import com.google.common.io.Files; public class GuavaIo { /** * 读取文件,list的size取决于文件内容的行数 * @param file * @return * @throws IOException */ public ListreadFile(File file) throws IOException{ if(!file.exists()){ return null; } List readLines = Files.readLines(file, Charsets.UTF_8); return readLines; } @Test public void testReadFile(){ try { List readFiles = readFile(new File("D:\\test.txt")); if(readFiles!=null&&!readFiles.isEmpty()){ for (String content : readFiles) { System.out.println(content); } } } catch (IOException e) { e.printStackTrace(); } } /** * 写文件 * @param contents * @param file * @throws IOException */ public void writeFile(String contents,File file) throws IOException{ Files.write(contents, file, Charsets.UTF_8); } @Test public void testWriteRile(){ try { writeFile("hhhhhhhh刘达宏",new File("D:\\test2.txt")); } catch (IOException e) { e.printStackTrace(); } } }