使用apache comments io读取hdfs文件

  1. package com.tiewoba.apache.comments;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.net.MalformedURLException;  
  7. import java.net.URL;  
  8. import java.util.Iterator;  
  9. import java.util.List;  
  10.   
  11. import org.apache.commons.io.IOUtils;  
  12. import org.apache.commons.io.LineIterator;  
  13. /** 
  14.  * 学习使用apache comments io包中的IOUtils中的方法 
  15.  * @author 王震 
  16.  * 
  17.  */  
  18. public class TestComment {  
  19.     public static void main(String[] args) throws MalformedURLException,  
  20.             IOException {  
  21.         InputStream in = new URL("http://www.apache.org").openStream();  
  22.         InputStream in2 = new URL("http://www.apache.org").openStream();  
  23.         /** 
  24.          * IOUtils.contentEquals(InputStream input1, InputStream input2) 
  25.          * 英文: 
  26.          * Compare the contents of two Streams to determine if they are equal or not. 
  27.          * 翻译: 
  28.          * 比较两个输入流的内容是否相等 
  29.          */  
  30.         try {  
  31.             //IOUtils.toString将缓冲区的内容以utf-8的编码方式以字符串的形式输出  
  32.             //System.out.println(IOUtils.toString(in,"UTF-8"));  
  33.         } finally {  
  34.             IOUtils.closeQuietly(in);  
  35.         }  
  36.         System.out.println(IOUtils.contentEquals(in, in2));  
  37.           
  38.         System.out.println("-----------------------------------------------------------------------");  
  39.           
  40.         /** 
  41.          * IOUtils.copy(InputStream input, OutputStream output) 
  42.          * 英文: 
  43.          * Copy bytes from an InputStream to an OutputStream. 
  44.          * 翻译: 
  45.          * 将字节从 InputStream复制到OutputStream中 
  46.          */  
  47.         ByteArrayOutputStream out = new ByteArrayOutputStream();  
  48.         IOUtils.copy(in2, out);  
  49.         System.out.println(out.size());  
  50.         //System.out.println(out.toString("UTF-8"));  
  51.         //IOUtils.closeQuietly(in);  
  52.         //IOUtils.closeQuietly(out);  
  53.         System.out.println("-----------------------------------");  
  54.         /**IOUtils.copyLarge(InputStream input, OutputStream output); 
  55.          * 英文 
  56.          * Copy bytes from a large (over 2GB) InputStream to an OutputStream. 
  57.          * 翻译: 
  58.          * 将字节超过2G的字节输入流复制到输出流中 
  59.          */  
  60.         IOUtils.copyLarge(in2, out);  
  61.         //System.out.println(out.toString("UTF-8"));  
  62.         System.out.println("------------------------------------------------------------");  
  63.           
  64.         /** 
  65.          * IOUtils.lineIterator(InputStream input, Charset encoding)  
  66.          * 英文 
  67.          * Return an Iterator for the lines in an InputStream, using the character encoding specified (or default encoding if null). 
  68.          * 翻译 
  69.          * 返回一个装有输入字节行数的Iterator对象,使用特定的字符编码(如果没用声明的话则用默认编码) 
  70.          */  
  71.         InputStream in3 = new URL("http://www.apache.org").openStream();  
  72.         try {  
  73.                LineIterator it = IOUtils.lineIterator(in3, "UTF-8");  
  74.                System.out.println(it.hasNext());  
  75.                if (it.hasNext()) {  
  76.                  String line = it.nextLine();  
  77.                  //System.out.println(line);  
  78.                }  
  79.         } finally {  
  80.                IOUtils.closeQuietly(in);  
  81.         }  
  82.         System.out.println("------------------------------------------------------------------");  
  83.         /** 
  84.          * IOUtils.read(InputStream input, byte[] buffer) 
  85.          * 英文: 
  86.          * Read bytes from an input stream. 
  87.          * 中文:从输入流中读取字节(通常返回输入流的字节数组的长度) 
  88.          */  
  89.         InputStream in4 = new URL("http://www.apache.org").openStream();  
  90.         byte[] buffer = new byte[100000];  
  91.         System.out.println(IOUtils.read(in4, buffer));  
  92.         System.out.println("------------------------------------------------");  
  93.         /** 
  94.          * IOUtils.readLines(InputStream input, Charset encoding)  
  95.          * 英文: 
  96.          * Get the contents of an InputStream as a list of Strings, one entry per line, using the specified character encoding. 
  97.          * 翻译: 
  98.          * 获得输入流的内容放回一个List<String>类型的容器,每一行为这个容器的一个入口,使用特定的字符集(如果为空就使用默认的字符集)   
  99.          */  
  100.         InputStream in5 = new URL("http://www.apache.org").openStream();  
  101.         List<String> list = IOUtils.readLines(in5, "UTF-8");  
  102.         Iterator<String> iter = list.iterator();  
  103.         while(iter.hasNext()){  
  104.             String s =  iter.next();  
  105.             //System.out.println(s);  
  106.         }  
  107.         System.out.println("--------------------------------------------------------------");  
  108.           
  109.   
  110.     }  
  111.   
  112. }  

猜你喜欢

转载自blog.csdn.net/qq_32253371/article/details/79212785