/** * @Description: * * @Title: FileGuava.java * @Package com.joyce.guava.main * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-6-26 下午01:18:18 * @version V2.0 */ package com.joyce.guava.main; import java.io.File; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.google.common.base.Charsets; import com.google.common.io.Files; /** * @Description:Guava的文件 * * @ClassName: FileGuava * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-6-26 下午01:18:18 * @version V2.0 */ public class FileGuava { public static void main(String[] args) { try { File readFile = new File(System.getProperty("user.dir") + "/src/resources/showarp.txt"); StringBuilder content = new StringBuilder(); if (readFile.exists()) { List<String> lines = readFile(readFile); for (String string : lines) { System.out.println(string); content.append(string + "\n"); } } File writeFile = new File(System.getProperty("user.dir") + "/src/resources/showarp" + new SimpleDateFormat("yyyyMMdd").format(new Date())+ ".txt"); writeFile(content.toString(), writeFile); } catch (Exception e) { e.printStackTrace(); } } /** * @Description: Guava文件读取 * * @param file * @return * * @Title: FileGuava.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-6-26 下午01:20:50 * @version V2.0 */ private static List<String> readFile(File file) throws Exception { if (!file.exists()) { return null; } return Files.readLines(file, Charsets.UTF_8); } /** * @Description: 从文件中获取有规则的数据 * * @param file * @return * * @Title: FileGuava.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-6-26 下午01:56:42 * @version V2.0 */ public List<String[]> readFileData(File file) throws Exception { List<String[]> list = new ArrayList<String[]>(); for (String rLine : readFile(file)) { list.add(rLine.split("\\s+")); } return list; } /** * @Description: Guava写文件 * * @param content * @param file * * @Title: FileGuava.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-6-26 下午01:32:06 * @version V2.0 */ private static void writeFile(String content, File file) throws Exception { if (!file.exists()) { file.createNewFile(); } Files.write(content, file, Charsets.UTF_8); } }
文件中的内容为:
代码执行后的效果:
并且将内容写到了另外一个文件中,用起来是不是很easy呢!
这领域真的是好东西特别多,就看大伙儿肯不肯动手动脑多学学!!!附上本人资源下载地址:http://download.csdn.net/download/luo201227/7581845
Google的Guava之IO升华,布布扣,bubuko.com
原文地址:http://blog.csdn.net/luo201227/article/details/36413279