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 + "\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升华的更多相关文章
- Google的Guava它Collection升华
至于Guava这是不是在这里说.一个已被提上一个非常特殊的! 这主要是为了分享Guava对于一些升华处理组.井,不多说了,直接在代码: package com.joyce.guava.bean; /* ...
- Guava 教程1-使用 Google Collections,Guava,static imports 编写漂亮代码
原文出处: oschina (API:http://ifeve.com/category/framework/guava-2/ JAR DOC Source 链接:http://pan.baidu.c ...
- Google的Guava类库简介(转)
说明:信息虽然有点旧,至少可以先了解个大概. Guava是一个Google的基于Java的类库集合的扩展项目,包括collections, caching, primitives support, c ...
- 有关google的guava工具包详细说明
Guava 中文是石榴的意思,该项目是 Google 的一个开源项目,包含许多 Google 核心的 Java 常用库. 目前主要包含: com.google.common.annotations c ...
- Google的Guava工具类splitter和apache stringutil对比 编辑
一直用的是apache的stringutil工具类,其实google的工具类项目 guava中居然也有字符串的分隔类splitter的,在 http://code.google.com/p/guava ...
- google中guava类库:AsyncEventBus
1.guava事件总线(AsyncEventBus)使用 1.1引入依赖 <dependency> <groupId>com.google.guava</groupId& ...
- spring中添加google的guava缓存(demo)
1.pom文件中配置 <dependencies> <dependency> <groupId>org.springframework</groupId> ...
- 使用 Google Guava 美化你的 Java 代码
文章转载自:http://my.oschina.net/leejun2005/blog/172328 目录:[ - ] 1-使用 GOOGLE COLLECTIONS,GUAVA,STATIC IMP ...
- 开源介绍:Google Guava、Google Guice、Joda-Time
一.Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency lib ...
随机推荐
- vue-router push
//push 方法一 对象 // this.$router.push({path:'HelloWorld2'}); //push 方法二 命名的路由 // this.$router.push({nam ...
- 【Excle数据透视表】如何在数据透视表中使用合并单元格标志
先有数据透视表如下: 现在看着这个格式不舒服,我们希望调整成如下这种样式 步骤 单击数据透视表任意单元格→右键→数据透视表选项→布局和格式→合并且居中排列带标签的单元格 注意:如果数据透视表报表布局不 ...
- php调用python
test_python.php <?php $k = $_REQUEST['k']; if (!empty($k)) { $k = trim($k); //$a 用来捕获输出 // 这里的arr ...
- leetcode第一刷_Binary Tree Zigzag Level Order Traversal
以出现的频率来看.树的层序遍历一定是考察的重点,除非工作人员想找题水数量. zigzag,还是有几道题的,层序的这个非常easy,假设是奇数层.reverse下面就可以.无他.我写的时候预计还不知道这 ...
- github上比較好的开源项目(持续更新)
1:https://github.com/Skykai521/StickerCamera 实现相机功能 实现对图片进行裁剪的功能 图片的滤镜功能 能为图片加入贴纸(贴纸可移动,放大,旋转) 能为图片加 ...
- Unity UGUI——提供可视功能的UI组件(Text)
基本属性介绍 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTXJfQUhhbw==/font/5a6L5L2T/fontsize/400/fill/I0J ...
- vmware下为CentOS7设置静态IP
vmware下为CentOS7设置静态IP可以分为三个主要步骤: 1. 在windows中设置VMWare创建的虚拟网络 2. 设置VMWare为静态IP 3. 设置CentOS7的网络为静态IP 首 ...
- u-boot中断功能初步分析之---------按键中断
作者:彭东林 邮箱:pengdonglin137@163.com QQ: 405728433 以前一直有个疑问,在U-boot下到底能不能使用中断,为了验证这个问题,于是乎,昨天晚上我在自己的 TQ2 ...
- 《TomCat与Java Web开发技术详解》(第二版) 第六章节的学习总结 ---- JSP技术
第六章主要介绍了JSP的相关知识. 1.JSP:是通过在HTML文件中加入java程序片段(Java Scriptlet)和JSP标记,就构成了JSP文件.JSP实质上是Servlet.JSP的API ...
- Unable to resolve address ' ' service ' ': Name or service not known
感谢朋友支持本博客,欢迎共同探讨交流,因为能力和时间有限,错误之处在所难免.欢迎指正. 假设转载,请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...