使用apache comments io读取hdfs文件
2018-01-31 08:37
447 查看
package com.tiewoba.apache.comments;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
/**
* 学习使用apache comments io包中的IOUtils中的方法
* @author 王震
*
*/
public class TestComment {
public static void main(String[] args) throws MalformedURLException,
IOException {
InputStream in = new URL("http://www.apache.org").openStream();
InputStream in2 = new URL("http://www.apache.org").openStream();
/**
* IOUtils.contentEquals(InputStream input1, InputStream input2)
* 英文:
* Compare the contents of two Streams to determine if they are equal or not.
* 翻译:
* 比较两个输入流的内容是否相等
*/
try {
//IOUtils.toString将缓冲区的内容以utf-8的编码方式以字符串的形式输出
//System.out.println(IOUtils.toString(in,"UTF-8"));
} finally {
IOUtils.closeQuietly(in);
}
System.out.println(IOUtils.contentEquals(in, in2));
System.out.println("-----------------------------------------------------------------------");
/**
* IOUtils.copy(InputStream input, OutputStream output)
* 英文:
* Copy bytes from an InputStream to an OutputStream.
* 翻译:
* 将字节从 InputStream复制到OutputStream中
*/
ByteArrayOutputStream out = new ByteArrayOutputStream();
IOUtils.copy(in2, out);
System.out.println(out.size());
//System.out.println(out.toString("UTF-8"));
//IOUtils.closeQuietly(in);
//IOUtils.closeQuietly(out);
System.out.println("-----------------------------------");
/**IOUtils.copyLarge(InputStream input, OutputStream output);
* 英文
* Copy bytes from a large (over 2GB) InputStream to an OutputStream.
* 翻译:
* 将字节超过2G的字节输入流复制到输出流中
*/
IOUtils.copyLarge(in2, out);
//System.out.println(out.toString("UTF-8"));
System.out.println("------------------------------------------------------------");
/**
* IOUtils.lineIterator(InputStream input, Charset encoding)
* 英文
* Return an Iterator for the lines in an InputStream, using the character encoding specified (or default encoding if null).
* 翻译
* 返回一个装有输入字节行数的Iterator对象,使用特定的字符编码(如果没用声明的话则用默认编码)
*/
InputStream in3 = new URL("http://www.apache.org").openStream();
try {
LineIterator it = IOUtils.lineIterator(in3, "UTF-8");
System.out.println(it.hasNext());
if (it.hasNext()) {
String line = it.nextLine();
//System.out.println(line);
}
} finally {
IOUtils.closeQuietly(in);
}
System.out.println("------------------------------------------------------------------");
/**
* IOUtils.read(InputStream input, byte[] buffer)
* 英文:
* Read bytes from an input stream.
* 中文:从输入流中读取字节(通常返回输入流的字节数组的长度)
*/
InputStream in4 = new URL("http://www.apache.org").openStream();
byte[] buffer = new byte[100000];
System.out.println(IOUtils.read(in4, buffer));
System.out.println("------------------------------------------------");
/**
* IOUtils.readLines(InputStream input, Charset encoding)
* 英文:
* Get the contents of an InputStream as a list of Strings, one entry per line, using the specified character encoding.
* 翻译:
* 获得输入流的内容放回一个List<String>类型的容器,每一行为这个容器的一个入口,使用特定的字符集(如果为空就使用默认的字符集)
*/
InputStream in5 = new URL("http://www.apache.org").openStream();
List<String> list = IOUtils.readLines(in5, "UTF-8");
Iterator<String> iter = list.iterator();
while(iter.hasNext()){
String s = iter.next();
//System.out.println(s);
}
System.out.println("--------------------------------------------------------------");
}
}
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
/**
* 学习使用apache comments io包中的IOUtils中的方法
* @author 王震
*
*/
public class TestComment {
public static void main(String[] args) throws MalformedURLException,
IOException {
InputStream in = new URL("http://www.apache.org").openStream();
InputStream in2 = new URL("http://www.apache.org").openStream();
/**
* IOUtils.contentEquals(InputStream input1, InputStream input2)
* 英文:
* Compare the contents of two Streams to determine if they are equal or not.
* 翻译:
* 比较两个输入流的内容是否相等
*/
try {
//IOUtils.toString将缓冲区的内容以utf-8的编码方式以字符串的形式输出
//System.out.println(IOUtils.toString(in,"UTF-8"));
} finally {
IOUtils.closeQuietly(in);
}
System.out.println(IOUtils.contentEquals(in, in2));
System.out.println("-----------------------------------------------------------------------");
/**
* IOUtils.copy(InputStream input, OutputStream output)
* 英文:
* Copy bytes from an InputStream to an OutputStream.
* 翻译:
* 将字节从 InputStream复制到OutputStream中
*/
ByteArrayOutputStream out = new ByteArrayOutputStream();
IOUtils.copy(in2, out);
System.out.println(out.size());
//System.out.println(out.toString("UTF-8"));
//IOUtils.closeQuietly(in);
//IOUtils.closeQuietly(out);
System.out.println("-----------------------------------");
/**IOUtils.copyLarge(InputStream input, OutputStream output);
* 英文
* Copy bytes from a large (over 2GB) InputStream to an OutputStream.
* 翻译:
* 将字节超过2G的字节输入流复制到输出流中
*/
IOUtils.copyLarge(in2, out);
//System.out.println(out.toString("UTF-8"));
System.out.println("------------------------------------------------------------");
/**
* IOUtils.lineIterator(InputStream input, Charset encoding)
* 英文
* Return an Iterator for the lines in an InputStream, using the character encoding specified (or default encoding if null).
* 翻译
* 返回一个装有输入字节行数的Iterator对象,使用特定的字符编码(如果没用声明的话则用默认编码)
*/
InputStream in3 = new URL("http://www.apache.org").openStream();
try {
LineIterator it = IOUtils.lineIterator(in3, "UTF-8");
System.out.println(it.hasNext());
if (it.hasNext()) {
String line = it.nextLine();
//System.out.println(line);
}
} finally {
IOUtils.closeQuietly(in);
}
System.out.println("------------------------------------------------------------------");
/**
* IOUtils.read(InputStream input, byte[] buffer)
* 英文:
* Read bytes from an input stream.
* 中文:从输入流中读取字节(通常返回输入流的字节数组的长度)
*/
InputStream in4 = new URL("http://www.apache.org").openStream();
byte[] buffer = new byte[100000];
System.out.println(IOUtils.read(in4, buffer));
System.out.println("------------------------------------------------");
/**
* IOUtils.readLines(InputStream input, Charset encoding)
* 英文:
* Get the contents of an InputStream as a list of Strings, one entry per line, using the specified character encoding.
* 翻译:
* 获得输入流的内容放回一个List<String>类型的容器,每一行为这个容器的一个入口,使用特定的字符集(如果为空就使用默认的字符集)
*/
InputStream in5 = new URL("http://www.apache.org").openStream();
List<String> list = IOUtils.readLines(in5, "UTF-8");
Iterator<String> iter = list.iterator();
while(iter.hasNext()){
String s = iter.next();
//System.out.println(s);
}
System.out.println("--------------------------------------------------------------");
}
}
相关文章推荐
- spark读取hdfs文件的路径使用正则表达式
- 使用FileSystem自带的api读取hdfs中的文件
- 使用Apache-FTPClient完成文件上传和从配置文件中读取properties的使用
- HDFS Java API使用之读取上传文件
- Hadoop 基本API 使用 -- 从HDFS文件系统 上读取文件
- 使用Apache的POI工具读取WORD文件...
- Spark中加载本地(或者hdfs)文件以及 spark使用SparkContext实例的textFile读取多个文件夹(嵌套)下的多个数据文件
- (9) Hadoop HDFS Java API使用 之 获取FileSystem对象 读取HDFS文件 写文件到HDFS
- 如何有效的使用C#读取文件
- 如何有效的使用C#读取文件
- 使用J2SE API读取Properties文件的六种方法(选择自 kindani 的 Blog )
- 使用apache common configuration读取配置文件或修改配置文件
- [导入]在 Visual Studio .NET IDE 内使用“从源代码管理打开”时出现无法读取项目文件的错误
- 如何有效的使用C#读取文件 (转)
- 使用J2SE API读取Properties文件的六种方法
- [导入]在 Visual Studio .NET IDE 内使用“从源代码管理打开”时出现无法读取项目文件的错误
- 使用序列化的方式来读取XML配置文件
- 读取存储在MsSql下的文件,配合使用DataGrid的模板列。
- 使用FileReader和FileWriter读取写入文件内容
- [导入]在 Visual Studio .NET IDE 内使用“从源代码管理打开”时出现无法读取项目文件的错误