java 读取输入流

简介:

Java 中如何读取输入流呢?

方式一:

Java代码   收藏代码
  1. /*** 
  2.      * Has been tested ok 
  3.      *  
  4.      * @param in 
  5.      * @return 
  6.      * @throws IOException 
  7.      */  
  8.     public static byte[] readBytes3(InputStream in) throws IOException {  
  9.         BufferedInputStream bufin = new BufferedInputStream(in);  
  10.         int buffSize = 1024;  
  11.         ByteArrayOutputStream out = new ByteArrayOutputStream(buffSize);  
  12.   
  13.         // System.out.println("Available bytes:" + in.available());  
  14.   
  15.         byte[] temp = new byte[buffSize];  
  16.         int size = 0;  
  17.         while ((size = bufin.read(temp)) != -1) {  
  18.             out.write(temp, 0, size);  
  19.         }  
  20.         bufin.close();  
  21.         in.close();  
  22.         byte[] content = out.toByteArray();  
  23.         out.close();  
  24.         return content;  
  25.     }  

 

方式二:

Java代码   收藏代码
  1. /*** 
  2.      * get byte[] from <code>InputStream</code> Low efficiency 
  3.      *  
  4.      * @param in 
  5.      * @return 
  6.      * @throws IOException 
  7.      */  
  8.     @Deprecated  
  9.     public static byte[] readBytes2(InputStream in) throws IOException {  
  10.         byte[] temp = new byte[1024];  
  11.         byte[] result = new byte[0];  
  12.         int size = 0;  
  13.         while ((size = in.read(temp)) != -1) {  
  14.             byte[] readBytes = new byte[size];  
  15.             System.arraycopy(temp, 0, readBytes, 0, size);  
  16.             result = mergeArray(result, readBytes);  
  17.         }  
  18.         return result;  
  19.     }  
  20. /*** 
  21.      * 合并字节数组 
  22.      *  
  23.      * @param a 
  24.      * @return 
  25.      */  
  26.     public static byte[] mergeArray(byte[]... a) {  
  27.         // 合并完之后数组的总长度  
  28.         int index = 0;  
  29.         int sum = 0;  
  30.         for (int i = 0; i < a.length; i++) {  
  31.             sum = sum + a[i].length;  
  32.         }  
  33.         byte[] result = new byte[sum];  
  34.         for (int i = 0; i < a.length; i++) {  
  35.             int lengthOne = a[i].length;  
  36.             if (lengthOne == 0) {  
  37.                 continue;  
  38.             }  
  39.             // 拷贝数组  
  40.             System.arraycopy(a[i], 0, result, index, lengthOne);  
  41.             index = index + lengthOne;  
  42.         }  
  43.         return result;  
  44.     }  

 

方式三:

Java代码   收藏代码
  1. /*** 
  2.      * 指定字符编码,无损地读取文本文件.推荐! 
  3.      *  
  4.      * @param in 
  5.      *            : 输入流,会关闭 
  6.      * @param charset 
  7.      *            : 字符编码 
  8.      * @return 
  9.      * @throws IOException 
  10.      */  
  11.     public static StringBuffer getFullContent3(InputStream in, String charset)  
  12.             throws IOException {  
  13.         StringBuffer sbuffer = new StringBuffer();  
  14.         InputStreamReader inReader;  
  15.         // 设置字符编码  
  16.         if (ValueWidget.isNullOrEmpty(charset)) {  
  17.             charset = SystemHWUtil.CURR_ENCODING;  
  18.         }  
  19.         inReader = new InputStreamReader(in, charset);  
  20.         char[] ch = new char[1024];  
  21.         int readCount = 0;  
  22.         while ((readCount = inReader.read(ch)) != -1) {  
  23.             sbuffer.append(ch, 0, readCount);  
  24.         }  
  25.         inReader.close();  
  26.         in.close();  
  27.         return sbuffer;  
  28.     }  

 

方式四:

Java代码   收藏代码
  1. /*** 
  2.      * 先读取出来字节数组,然后在包装成为字符串;效率不高,因为有拷贝操作(System.arraycopy) 
  3.      *  
  4.      * @param in 
  5.      * @param charset 
  6.      * @return 
  7.      * @throws IOException 
  8.      */  
  9.     public static String getFullContent2(InputStream in, String charset)  
  10.             throws IOException {  
  11.         //并不是要读取的字节的长度  
  12.         int step = BUFFSIZE_1024;  
  13.         BufferedInputStream bis = new BufferedInputStream(in);  
  14.   
  15.         // Data's byte array  
  16.         byte[] receData = new byte[step];  
  17.   
  18.         // data length read from the stream  
  19.         int readLength = 0;  
  20.   
  21.         // data Array offset  
  22.         int offset = 0;  
  23.   
  24.         // Data array length  
  25.         int byteLength = step;  
  26.   
  27.         while ((readLength = bis.read(receData, offset, byteLength - offset)) != -1) {  
  28.             // Calculate the current length of the data  
  29.             offset += readLength;  
  30.             // Determine whether you need to copy data , when the remaining  
  31.             // space is less than step / 2, copy the data  
  32.             if (byteLength - offset <= step / 2) {  
  33.                 byte[] tempData = new byte[receData.length + step];  
  34.                 System.arraycopy(receData, 0, tempData, 0, offset);  
  35.                 receData = tempData;  
  36.                 byteLength = receData.length;  
  37.             }  
  38.         }  
  39.   
  40.         return new String(receData, 0, offset, charset);  
  41.     }  

 

方式五:

Java代码   收藏代码
  1. /*** 
  2.      * write inputstream into outputStream ,haven't close stream. 
  3.      *  
  4.      * @param ins 
  5.      * @param outs 
  6.      */  
  7.     public static void writeIn2Output(InputStream ins, OutputStream outs,  
  8.             boolean isCloseOut, boolean isCloseInput) {  
  9.         try {  
  10.             int resultInt = -1;  
  11.             byte[] bytes = null;  
  12.             bytes = new byte[4096];  
  13.   
  14.             try {  
  15.                 while ((resultInt = ins.read(bytes)) != -1) {  
  16.                     outs.write(bytes, 0, resultInt);  
  17.                 }  
  18.             } catch (IOException e) {  
  19.                 e.printStackTrace();  
  20.             } finally {  
  21.                 if (isCloseOut) {  
  22.                     try {  
  23.                         outs.close();  
  24.                     } catch (IOException e) {  
  25.                         e.printStackTrace();  
  26.                     }  
  27.                 }  
  28.                 if (isCloseInput) {  
  29.                     try {  
  30.                         ins.close();  
  31.                     } catch (IOException e) {  
  32.                         e.printStackTrace();  
  33.                     }  
  34.                 }  
  35.             }  
  36.         } finally {  
  37.   
  38.         }  
  39.     }  

 

方式六:

Java代码   收藏代码
  1. /*** 
  2.      * write inputstream into file according to specified length. 
  3.      *  
  4.      * @param file 
  5.      * @param ins 
  6.      * @param length2 
  7.      * @throws IOException 
  8.      */  
  9.     public static void writeInputStream2File(File file, InputStream ins,  
  10.             long length2, boolean isShutOutputStream) throws IOException {  
  11.         String parentDir = SystemHWUtil.getParentDir(file.getAbsolutePath());  
  12.         File fatherFile = new File(parentDir);  
  13.         if (!fatherFile.exists()) {  
  14.             fatherFile.mkdirs();  
  15.         }  
  16.         FileOutputStream outs = new FileOutputStream(file);  
  17.         int readSize;  
  18.         byte[] bytes = null;  
  19.         bytes = new byte[(int) length2];  
  20.   
  21.         long length_tmp = length2;  
  22.         while ((readSize = ins.read(bytes)) != -1) {  
  23.             length_tmp -= readSize;  
  24.   
  25.             outs.write(bytes, 0, readSize);  
  26.             if (length_tmp == 0) {  
  27.                 break;  
  28.             }  
  29.             if (length_tmp < 4096) {  
  30.                 bytes = new byte[(int) length_tmp];  
  31.             }  
  32.         }  
  33.         if (isShutOutputStream) {  
  34.             outs.close();  
  35.         }  
  36.   
  37.     }  

 

方式七:

Java代码   收藏代码
  1. /*** 
  2.      * 从输入流获取字节数组 
  3.      * @param br_right 
  4.      * @param length2 
  5.      * @return 
  6.      * @throws IOException 
  7.      */  
  8.     public static byte[] readBytesFromInputStream(BufferedInputStream br_right,  
  9.             int length2) throws IOException {  
  10.         int readSize;  
  11.         byte[] bytes = null;  
  12.         bytes = new byte[length2];  
  13.   
  14.         long length_tmp = length2;  
  15.         long index =0;//start from zero  
  16.         while ((readSize = br_right.read(bytes,(int)index,(int)length_tmp)) != -1) {  
  17.             length_tmp -= readSize;  
  18.             if (length_tmp == 0) {  
  19.                 break;  
  20.             }  
  21.             index=index+readSize;  
  22.         }  
  23.         return bytes;  
  24.     }  

 

相关文章
|
8月前
|
缓存 网络协议 Java
深入理解 Java 中的 InputStream:输入流的奥秘解析
在 Java 编程中,输入流(InputStream)是一个重要的概念,它为我们提供了一种从数据源读取数据的方式。无论是读取文件、网络数据还是其他数据源,InputStream 都是不可或缺的工具。本文将带您深入探索 Java 中的 InputStream,解析其原理、用法以及在实际开发中的应用
|
8月前
|
Java
Java IO流之访问文件的字节输入流FileInputStream和字节输入流FileOutputStream的详解
Java IO流之访问文件的字节输入流FileInputStream和字节输入流FileOutputStream的详解
71 0
Java-读取本地txt文件的问题
Java-读取本地txt文件的问题
187 0
|
Java 数据库
java语言读取Excel文件信息
最近项目中有一个表信息过大,需要手动删除,因为信息量比较大手动删除较为麻烦,所以采用使用excel读取数据的方式,然后操作数据库删除数据,其实只要是想要从Excel中获取信息都可以使用这种方式进行操作Excel,代码很简单。
186 0
Java基础进阶IO流-FileInputStream字节输入流
Java基础进阶IO流-FileInputStream字节输入流
Java基础进阶IO流-FileInputStream字节输入流
|
缓存 JSON Java
java 实现读取txt文件,反射创建对象,android 手机缓存文件目录
java 实现读取txt文件,反射创建对象,android 手机缓存文件目录
339 1
java 实现读取txt文件,反射创建对象,android 手机缓存文件目录
Java如何读取输入
记录下Java如何读取输入
216 0
Java如何读取输入
JAVA远程读取服务器文件
JAVA远程读取服务器文件
624 1