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.     }  

 

时间: 2024-08-02 19:35:10

java 读取输入流的相关文章

java的输入流

当然,我们经常想做的一件事情是将格式化的输出打印到控制台,但那已在第5章创建的com.bruceeckel.tools中得到了简化. 第1到第4部分演示了输入流的创建与使用(尽管第4部分展示了将输出流作为一个测试工具的简单应用). 1. 缓冲的输入文件 为打开一个文件以便输入,需要使用一个FileInputStream,同时将一个String或File对象作为文件名使用.为提高速度,最好先对文件进行缓冲处理,从而获得用于一个BufferedInputStream的构建器的结果句柄.为了以格式化的

[转Java读取文件]各种流

[Java]读取文件方法大全 1.按字节读取文件内容 2.按字符读取文件内容 3.按行读取文件内容  4.随机读取文件内容 public class ReadFromFile {     /**      * 以字节为单位读取文件,常用于读二进制文件,如图片.声音.影像等文件.      */     public static void readFileByBytes(String fileName) {         File file = new File(fileName);     

java读取pdf乱码-java读取pdf文件出现中文乱码

问题描述 java读取pdf文件出现中文乱码 代码如下,读取pdf文件时,出现中文字符乱码的情况,求大神解决.. package read; import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream; import com.linuxense.javadbf.DBFField;import com.linuxense.javadbf.DBFReader; public class re

java读取文件和写入文件的方式(简单实例)_java

Java代码 public class ReadFromFile { /** * 以字节为单位读取文件,常用于读二进制文件,如图片.声音.影像等文件. */ public static void readFileByBytes(String fileName) { File file = new File(fileName); InputStream in = null; try { System.out.println("以字节为单位读取文件内容,一次读一个字节:"); // 一次读

Java 读取文件方法大全_java

1.按字节读取文件内容 public class ReadFromFile { public static void readFileByBytes(String fileName) { File file = new File(fileName); InputStream in = null; try { System.out.println("以字节为单位读取文件内容,一次读一个字节:"); // 一次读一个字节 in = new FileInputStream(file); in

简单的java读取文件示例分享_java

可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了 通过这条线路读取甲方的信息:new FileInputStream(file) 目前这个信息已经读进来内存当中了.接下来需要解读成乙方可以理解的东西 既然你使用了FileInputStream().那么对应的需要使用InputStreamReader()这个方法进行解读刚才装进来内存当中的数据 解读完成后要输出呀.那当然要转换成IO可以识别

java读取用户登入退出日志信息上传服务端_java

本文实例为大家分享了读取用户登入出日志并上传服务端的具体实现代码,供大家参考,具体内容如下 该客户端运行在给用户提供unix服务的服务器上.用来读取并收集该服务器上用户的上下线信息,并进行配对整理后发送给服务端汇总. 具体实现代码: 1. DMSServer.java package com.dms; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.i

Java读取txt文件和写入txt文件的简单实例_java

写Java程序时经常碰到要读如txt或写入txt文件的情况,但是由于要定义好多变量,经常记不住,每次都要查,特此整理一下,简单易用,方便好懂! package edu.thu.keyword.test; import java.io.File; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream;

Java读取大文件

问题描述 java读取超大文件,(5G左右),并且每行读出来,需要处理下再重新写入一个文件,大家有碰到过的吗?求指点 解决方案 import java.io.*;public class ReadBigFileLineByLine{ public static void main(String[] args) { try{ //Big file to read String fileName = "MyBigFile.txt"; FileReader fileReader = new