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 ...
随机推荐
- 拒绝IP登陆
tail -n 30 /var/log/messages 发现很多IP尝试登陆,直接封禁. 解决方案:1. vi /etc/hosts.allow 添加 sshd:143.63.182.238 [注意 ...
- shell2
例一:数组选择 #!/bin/sh menu=("Apple" "Grape" "Orange") PS3="喜欢哪个" ...
- 在PreferenceAcitity中使用Fragement时避免额外的Left和RightPadding
On Android 4.4 遇到过这种问题: 注意到.上面的ActionBar部分的左右各有48像素的padding. 要了解该问题的成因,要首先了解其结构: 该页面的Activity是一个Pref ...
- 关于\r和\n的一些问题总结
\r表示"回车"(carriage return).\n表示"换行"(line feed),在Windows系统下.输入回车键会自己主动变成\r\n 相同的,在 ...
- 超高逼格Log日志打印
代码地址如下:http://www.demodashi.com/demo/12646.html 前言 Log日志的打印一直是一个比较头疼的事,怎样才能让自己的log显示更多信息,怎样才能让自己的log ...
- oracle 序列重置
问题一:怎样重置oracle序列 oracle序列创建以后,假设想重置序列从 0 開始,逐渐递增1,能够採用例如以下存储过程: create or replace procedure reset_se ...
- 前端自动化Grunt教程
最近在学习Bootstrap,了解一部分之后,发现Bootstrap使用了LESS,好嘛,开始学习LESS,LESS了解部分,发现自动编译工具Grunt,了解Grunt过程发现需要使用node.js的 ...
- 会话管理之session技术
上一节我们总结了cookie技术,这节主要总结一下session技术. 1. session对象 在web开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占 ...
- Atitit..jdk java 各版本新特性 1.0 1.1 1.2 1.3 1.4 1.5(5.0) 1.6(6.0) 7.0 8.0 9.0 attilax 大总结
Atitit..jdk java 各版本新特性 1.0 1.1 1.2 1.3 1.4 1.5(5.0) 1.6(6.0) 7.0 8.0 9.0 attilax 大总结 1.1. Java的编年史2 ...
- nl 命令
nl命令在linux系统中用来计算文件中行号.nl 可以将输出的文件内容自动的加上行号!其默认的结果与 cat -n 有点不太一样, nl 可以将行号做比较多的显示设计,包括位数与是否自动补齐 0 等 ...