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 ...
随机推荐
- PS 如何制作环绕文字效果
最终效果 地球素材 1.打开素材,使用椭圆选区工具按住shift绘制正圆选区 2.转到路径面板,将选区变为工作路径 3.选择文字工具,在路径上输入文字 4.ctrl+T,按住ctrl+alt,鼠标拖动 ...
- node - 上传文件并且修改名称
html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- Laravel服务/服务提供者/容器/契约和门面
1.服务是什么? 服务是提供了一些功能的类,比如发送邮件,写日志. 2.Laravel服务提供者是什么? 服务提供者中指明了这个提供者可以提供哪些服务(注册服务),以及服务注册后默认调用一些方法(bo ...
- 使用 react-native-tab-navigator 创建 TabBar 组件
1.首先安装好ReactNative的运行环境,安装组件依赖库 使用npm install react-native-tab-navigator --save安装所依赖的第三方库 2.导入 impor ...
- DNS、bind 953端口
1.953端口是rndc 的端口 2.rndc是监控bind的统计数据用的,同时不需要为了更新某个zone而重启bind 3.输入rndc ,如果ok的话,是这样的 4.看到第一条语句了没. 不需要重 ...
- ExtJs的Ext.grid.GridPanel不能选择复制表格中的内容解决方案
今天遇到grid复制的问题,在网上找到了一个解决办法,只需改下CSS和JS,给大家分享一下: 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/dy_paradise/a ...
- oracle经常使用函数(1)
1.返回与指定的字符相应的十进制数 select ascii('A') A,ascii('z') a,ascii('12') 一打,ascii(' ') kg from dual; 2.返回与指定十进 ...
- Java注解基本介绍
注解(Annotation),又称元数据(MetaData),提供了一种在代码中添加信息的形式化的方法,将元数据和源代码结合在一起. 1. 外部配置文件如XML存在的问题: 代码复杂度较高,需要编写很 ...
- oc 经常用到弹出view的方法
#pragma mark 弹出view -(void)exChangeOut:(UIView *)changeOutView dur:(CFTimeInterval)dur { CAKeyframeA ...
- 工作总结 1 sql写法 insert into select from 2 vs中 obj文件和bin文件 3 npoi 模板copy CopySheet 最好先全部Copy完后 再根据生成sheet写数据 4 sheet.CopyRow(rowsindex, rowsindex + x); 5 npoi 复制模板如果出现单元格显示问题
我们可以从一个表中复制所有的列插入到另一个已存在的表中: INSERT INTO table2SELECT * FROM table1; 或者我们可以只复制希望的列插入到另一个已存在的表中: INSE ...