- Mastering Java for Data Science
- Alexey Grigorev
- 221字
- 2021-07-02 23:44:35
Commons IO
Like Apache Commons Lang extends the java.util standard package, Apache Commons IO extends java.io; it is a Java library of utilities to assist with I/O in Java, which, as we previously learned, can be quite verbose.
To include the library in your project, add the dependency snippet to the pom.xml file:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
We already learned about Files.lines from Java NIO. While it is handy, we do not always work with files, and sometimes need to get lines from some other InputStream, for example, a web page or a web socket.
For this purpose, Commons IO provides a very helpful utility class IOUtils. Using it, reading the entire input stream into string or a list of strings is quite easy:
try (InputStream is = new FileInputStream("data/text.txt")) {
String content = IOUtils.toString(is, StandardCharsets.UTF_8);
System.out.println(content);
}
try (InputStream is = new FileInputStream("data/text.txt")) {
List<String> lines = IOUtils.readLines(is, StandardCharsets.UTF_8);
System.out.println(lines);
}
Although we use FileInputStream objects in this example, it can be any other stream. The first method, IOUtils.toString, is particularly useful, and we will use it later for crawling web pages and processing the responses from web services.
There are a lot more useful methods for I/O in this library, and to get a good overview, you can consult the documentation available at https://commons.apache.org/io.
- 數(shù)據(jù)挖掘原理與實(shí)踐
- Effective Amazon Machine Learning
- 計(jì)算機(jī)信息技術(shù)基礎(chǔ)實(shí)驗(yàn)與習(xí)題
- 基于Apache CXF構(gòu)建SOA應(yīng)用
- Remote Usability Testing
- Spark大數(shù)據(jù)編程實(shí)用教程
- 探索新型智庫發(fā)展之路:藍(lán)迪國際智庫報(bào)告·2015(上冊)
- 智慧城市中的大數(shù)據(jù)分析技術(shù)
- AndEngine for Android Game Development Cookbook
- 智能與數(shù)據(jù)重構(gòu)世界
- 云工作時(shí)代:科技進(jìn)化必將帶來的新工作方式
- Rust High Performance
- Swift Functional Programming(Second Edition)
- 數(shù)據(jù)中臺實(shí)戰(zhàn):手把手教你搭建數(shù)據(jù)中臺
- 大數(shù)據(jù)技術(shù)體系詳解:原理、架構(gòu)與實(shí)踐