针对文本文件到Java列表中使用Commons或Guava和java将文本写入文件这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展common,GoogleGuava,Guice、Commo
针对文本文件到Java列表中 使用Commons或Guava和java将文本写入文件这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展common,Google Guava,Guice、Commons Configuration 2.0 发布,Java 配置文件读写、Commons Configuration 2.1 发布,Java 配置文件读写、Commons Configuration 2.1.1 发布,Java 配置文件读写等相关知识,希望可以帮助到你。
本文目录一览:- 文本文件到Java列表中 使用Commons或Guava(java将文本写入文件)
- common,Google Guava,Guice
- Commons Configuration 2.0 发布,Java 配置文件读写
- Commons Configuration 2.1 发布,Java 配置文件读写
- Commons Configuration 2.1.1 发布,Java 配置文件读写
文本文件到Java列表中 使用Commons或Guava(java将文本写入文件)
使用Commons或Guava库,将每行文本(来自文本文件)放入LinkedList(作为String对象)或某些其他集合的最优雅方法是什么。
common,Google Guava,Guice
common,Google Guava,Guice 博客分类: java1、apache common http://commons.apache.org
2、Google Guava http://code.google.com/p/guava-libraries/
3、Google DI框架 Guice http://code.google.com/p/google-guice/
1、一提到common库,大家第一个肯定Apache有个org.apache.common.*的集合(http://commons.apache.org),里面继承了各种数据结构、文件IO、网络IO、Hash、断言等等很多东西。大致列出了一个比较常用的:
codec : 编码解码算法,如Base64、UrlEncode等
Collections : 扩展jdk的collections集合
Compress : zip、tar等压缩算法
Deamon : Unix后台daemon精灵进程
Dbcp : 可扩展的数据库连接池
JCS : Java带LRU的Cache,可以看成是一个固定大小可自动淘汰key的Map
Pool : “池”化的实现,例如数据库连接池、线程池、对象池都可以基于此进行扩展
可以看出,其实很多功能性的,平时开发中很可能遇到的但jdk中不提供的,一般都可以找到。或者直接用或者直接扩展成自己的类。一般能从中找到的就不要自己实现,因为不一定更好,而且还需要时间的考验。关于Apache Common开发过java的都不陌生,如果你用了SSH就间接的使用了它。这里不做过多讨论了,如果有兴趣可以针对细节再留言探讨哈。
2、Apache Common是一个时间比较久的框架了,Google针对基础框架退出了自己的类库,并且开源出来(http://code.google.com/p/guava-libraries/),名为“Guava”。它在部分功能上其实是ApacheCommon的一个子集,但在性能上做了很多优化,并且针对并发和大规模系统开发做了很多新的策略(如CopyOnWrite、Immutable、SkipList)等。虽然有些类和java.util.concurrent有些重叠,但是在一般环境下都可以替代。
Google比较喜欢java的"链式编程“,很多类都是支持链式的,比如Joiner.on("#").skipNulls().join("a","b","c")。不太熟悉的同学可以Google之。本文针对其中几个特别的实现,以及适用场景进行讲解:
Preconditions : 类似Assert的预言,但处理方式是抛出异常,比如checkNotNull(obj),如果obj是null则会抛出NullPtrException
ImmutableSet/Map : 不可变的set和map,存储constant常量的集合,元素内容和个数不再变化,这里就可以做很多空间和检索上的优化。
BiMap : jdk的map一般是通过key检索value,如果反查的话,最简单的就是构造另一个同步的map把value存成key,而BiMap支持双向检索,通过reverse反转关系。
Table : 支持二维索引的Map,比如我们有个Map<String,Map<String,Integer>>,通过两个字符串定位int,使用起来很麻烦,Table支持row和column的概念,在横“向”和“纵向”两个纬度来索引。
RangeSet : 区间型的集合,比如想得到一个1~100的数字的集合,最简单的做法就是for循环100次然后put进容器。rangeset支持add(range.open(1,100))这样类似php range()函数一样。
ListenableFuture : jdk提供了一个java.util.concurrent.Future的异步操作,可以非阻塞的执行一个Runable任务,但Future有一个问题,必须同步的调用get()取等结果或者直接获得结果,在逻辑上还是需要同步的操作,ListenableFuture通过addListener()的方式直接“回调”实现的方法,可以做到完全异步。
StringJoiner : 如果用过python的同学,应该很喜欢字符串的join()函数,实现字符串的按规律拼接。如
- Joiner.on("#").skipNulls().join("a","b","c");// 返回"a#b#c"
Joiner.on("#").skipNulls().join("a","b","c");// 返回"a#b#c"
Hashing : 如果用过Java自带的MD5的同学,经常看到比较繁琐的代码,导入security下好多包,然后静态初始化,然后getInstance获得算法实例。而Guava的md5非常干净,只需要:
- Hashing.md5().newHasher().put("123123123");
Hashing.md5().newHasher().put("123123123");
BloomFilter : 布隆过滤,可通过设置希望的准确率,数据总量来初始化 :
- BloomFilter<Person> bf = BloomFilter.create(object, 500, 0.01);// 0.01为可能错误判断的概率
BloomFilter<Person> bf = BloomFilter.create(object, 500, 0.01);// 0.01为可能错误判断的概率
EventBus : 接口简单的pub/sub的只需要一个register和Subcribe的注解即可。
3、还有一个Google的common框架不得不提,就是Google的依赖注入(Dependency Injector)框架Guice,这是个Google正在大规模使用的lib。主要提供代码级的依赖注入和AOP切面。
Apache的Spring可谓是DI的经典之作,但由于其功能太多,而且和ssh框架绑定比较多,如果只使用DI和AOP,那么还是推荐大家使用Guice。而且Guice不像Spring是用xml来描述,是通过注解来注入,所以做到了动态注入(代码可控)。给出一个DI的例子:
- public class HelloWorld implements HelloWorld {
- @Override
- String sayHello() {return "hello world";}
- }
public class HelloWorld implements HelloWorld {
@Override
String sayHello() {return "hello world";}
}
- @ImplementedBy(HelloWorldImpl.class)
- public interface HelloWorld {
- String sayHello();
- }
@ImplementedBy(HelloWorldImpl.class)
public interface HelloWorld {
String sayHello();
}
- Injector inj= Guice.createInjector();
- HelloWorld hw = inj.getInstance(HelloWorld.class);
- System.out.println(hw.sayHello());
转载至;http://blog.csdn.net/gugemichael/article/details/8701150
Commons Configuration 2.0 发布,Java 配置文件读写
Commons Configuration 2.0 发布了,Commons Configuration 是一个 java 应用程序的配置管理类库。可以从 properties 或者 xml 文件中加载软件的配置信息,用来构建支撑软件运 行的基础环境。在一些配置文件较多较的复杂的情况下,使用该配置工具比较可以简化配置文件的解析和管理。也提高了开发效率和软件的可维护性。
更新日志:
CONFIGURATION-621:
Moved ConfigurationLogger class to io package to avoid cyclic dependencies between packages.CONFIGURATION-619:
CombinedConfigurationBuilder now supports inheritance of its parameters to child configuration sources. This is enabled by default.CONFIGURATION-615:
Changed generic types in the signatures of a MapConfiguration constructor and AbstractConfiguration.getList(String, List). These changes were made in version 1.10 as fixes for CONFIGURATION-557 and CONFIGURATION-558. But it had been missed to merge them to trunk.
修复 bug:
CONFIGURATION-620:
Fixed two invalid examples in the user''s guide for file-based configurations.CONFIGURATION-618:
When using immutable configurations exceptions thrown by the wrapped configuration came out as UndeclaredThrowableException. This has been fixed; now the correct original exception is thrown.
Commons Configuration 2.1 发布,Java 配置文件读写
Apache Commons Configuration 2.1 发布了,Apache Commons Configuration 是 Apache 提供的一个组件,用于读取保存有配置参数的 XML 文件。该版本是 Apache Commons Configuration 2.x 的首个维护版,修复了一些 bug,也提供了一些新功能,例如对于 Spring 框架更好的整合。要注意的是,这个组件需要在 Java 1.6 或更高的版本上使用。
该版本更新如下:
New Features
CONFIGURATION-636
PropertiesConfigurationLayout now allows manipulating the order of keys when the properties file is written.
CONFIGURATION-624
Support Commons Configuration as PropertySource in Spring.
Fixed Bugs
CONFIGURATION-634
HomeDirectoryLocationStrategy now works correctly in the mode that evaluates the FileLocator''s base path.
CONFIGURATION-633
Interpolation was improved to better support properties with multiple values.
CONFIGURATION-632
The methods getStringArray() and getList() of CompositeConfiguration now support the interpolation of variables that reference properties with multiple values.
CONFIGURATION-622
Fixed a problem in INIConfiguration.write() with keys containing a separator character. This separator had been duplicated. Such keys are now handled correctly when the configuration is saved.
Other changes
CONFIGURATION-628
Support for the ant build was dropped.
CONFIGURATION-626
ImmutableConfiguration.getArray() has been deprecated. Arrays can now be queried using the generic get() method in a type-safe way.
CONFIGURATION-631
Updated optional dependency to Apache Commons VFS from 2.0 to 2.1. (The older version still works.)
CONFIGURATION-635
Updated optional dependency to Apache Commons Codec from 1.9 to 1.10. (The older version still works.)
查看官方更新说明以了解更多:http://www.apache.org/dist/commons/configuration/RELEASE-NOTES.txt
下载地址:http://commons.apache.org/proper/commons-configuration/download_configuration.cgi
主页:http://www.apache.org/dist/commons/configuration/
Commons Configuration 2.1.1 发布,Java 配置文件读写
Apache Commons Configuration 2.1.1 发布了。
Apache Commons Configuration 是 Apache 提供的一个组件,用于读取保存有配置参数的 XML 文件。该版本是 Apache Commons Configuration 2.x 的首个维护版,修复了一些 bug,也提供了一些新功能,例如对于 Spring 框架更好的整合。要注意的是,这个组件需要在 Java 1.6 或更高的版本上使用。
更新内容:
bug 修复
改进了在 XMLConfiguration 中由具有定界符字符串的字符串定义的列表的处理。 保存配置时,此类列表现在保留其原始格式。
使 PropertyBuilderParameter 的 IOFactory 属性与最新版本的 Commons BeanUtils 兼容。 此版本更改了以多个大写字母开头的属性的处理。
PropertiesConfiguration 现在可以与自动保存模式正常工作。
如果加载了具有另一个注释的文件,PropertiesConfigurationLayout 不再复制标题注释。
用户指南文档改进。
改进了 FileBased 接口的文档。 Javadocs 现在明确地声明这些方法不应该被客户端代码调用。 实现 FileLocatorAware 的配置类中的异常处理已得到改进。
PropertiesConfiguration 现在可以正确地解除在 Java 属性文件中始终转义的一些特殊字符。
其他更改
在 Commons 配置包的 OSGi 绑定清单中,Spring 包的包导入现在标记为可选。
改进了单元测试中临时文件和文件夹的处理。
将 Commons BeanUtils 的依赖项更新到 1.9.3 版本。 这修复了在使用 ConfigurationBuilder 时记录的异常 stacktrace 的问题。
更多详情查看发行日志
下载地址:
commons-configuration2-2.1.1-src.tar.gz
commons-configuration2-2.1.1-src.zip
我们今天的关于文本文件到Java列表中 使用Commons或Guava和java将文本写入文件的分享就到这里,谢谢您的阅读,如果想了解更多关于common,Google Guava,Guice、Commons Configuration 2.0 发布,Java 配置文件读写、Commons Configuration 2.1 发布,Java 配置文件读写、Commons Configuration 2.1.1 发布,Java 配置文件读写的相关信息,可以在本站进行搜索。
本文标签: