• Google的Guava之IO升华


    版权声明:本文为博主原创文章,未经博主同意不得转载。

    https://blog.csdn.net/luo201227/article/details/36413279

    程序员在开发过程中,使用文件的几率是相当大的,有时候,我们甚至须要几十秒内读取一下IO流中的数据。可是原生态的文件流的读写,一旦操作不当,就有可能出现内存溢出和打开文件数过多的异常错误,这一点在Linux环境下表现得尤其突出,所以使用好原生态的读写文件流真的非常重要!好啦,这里着重来讲一下Google的Guava对IO的操作升级。上一篇讲的Guava对Collection的优化,魅力之处尽在不言中了!

    Ok,咱就上代码了!这里使用文件来做Demo:

    /**
     * @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 + "
    ");
    				}
    			}
    			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

  • 相关阅读:
    【洛谷4657】[CEOI2017] Chase(一个玄学的树形DP)
    Tarjan在图论中的应用(二)——用Tarjan来求割点与割边
    Tarjan在图论中的应用(一)——用Tarjan来实现强连通分量缩点
    jquery放大镜
    自定义上传按钮样式
    一些设计理论资料
    jquery滚动条
    全栈工程师到底有什么用(转)
    巧用CSS文件愚人节恶搞(转)
    仿双色球-随机产生7个数字
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10815534.html
  • Copyright © 2020-2023  润新知