gaojingsong 阅读(121) 评论(0)

InputStream提供的是字节流的读取,而非文本读取,用Reader读取出来的是char数组或者String ,使用InputStream读取出来的是byte数组。

 Reader类及其子类提供的字符流的读取char,inputStream及其子类提供字节流的读取byte,所以 FileReader类是将文件按字符流的方式读取,FileInputStream则按字节流的方式读取文件;InputStreamReader可以将读如stream转换成字符流方式,是reader和stream之间的桥梁

 

package cn.guolf.activiti;

 

import java.io.BufferedReader;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.StringBufferInputStream;

import java.io.UnsupportedEncodingException;

 

@SuppressWarnings("deprecation")

public class Demo {

 

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

 

}

 

public String getStrFromInputSteam(InputStream in){  

    BufferedReader bf = null;

try {

bf = new BufferedReader(new InputStreamReader(in,"UTF-8"));

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}  

    //最好在将字节流转换为字符流的时候 进行转码  

    StringBuffer buffer=new StringBuffer();  

    String line="";  

    try {

while((line=bf.readLine())!=null){  

    buffer.append(line);  

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}  

      

   return buffer.toString();  

 

/**

* 利用BufferedReader实现Inputstream转换成String <功能详细描述>

* @param in

* @return String

*/

 

public static String Inputstr2Str_Reader(InputStream in, String encode) {

 

String str = "";

try {

if (encode == null || encode.equals("")) {

// 默认以utf-8形式

encode = "utf-8";

}

BufferedReader reader = new BufferedReader(new InputStreamReader(

in, encode));

StringBuffer sb = new StringBuffer();

 

while ((str = reader.readLine()) != null) {

sb.append(str).append("\n");

}

return sb.toString();

} catch (UnsupportedEncodingException e1) {

e1.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

 

return str;

}

 

/**

* 利用byte数组转换InputStream------->String <功能详细描述>

* @param in

* @return

* @see [类、类#方法、类#成员]

*/

 

public static String Inputstr2Str_byteArr(InputStream in, String encode) {

StringBuffer sb = new StringBuffer();

byte[] b = new byte[1024];

int len = 0;

try {

if (encode == null || encode.equals("")) {

// 默认以utf-8形式

encode = "utf-8";

}

while ((len = in.read(b)) != -1) {

sb.append(new String(b, 0, len, encode));

}

return sb.toString();

} catch (IOException e) {

e.printStackTrace();

}

return "";

 

}

 

/**

* 利用ByteArrayOutputStream:Inputstream------------>String <功能详细描述>

* @param in

* @return

* @see [类、类#方法、类#成员]

*/

public static String Inputstr2Str_ByteArrayOutputStream(InputStream in,

String encode) {

ByteArrayOutputStream out = new ByteArrayOutputStream();

byte[] b = new byte[1024];

int len = 0;

try {

if (encode == null || encode.equals("")) {

// 默认以utf-8形式

encode = "utf-8";

}

while ((len = in.read(b)) > 0) {

out.write(b, 0, len);

}

return out.toString(encode);

} catch (IOException e) {

e.printStackTrace();

}

return "";

}

 

/**

* 利用ByteArrayInputStream:String------------------>InputStream <功能详细描述>

* @param inStr

* @return

* @see [类、类#方法、类#成员]

*/

public static InputStream Str2Inputstr(String inStr) {

try {

// return new ByteArrayInputStream(inStr.getBytes());

// return new ByteArrayInputStream(inStr.getBytes("UTF-8"));

return new StringBufferInputStream(inStr);

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

 

public InputStream getInputStreamFromString(String str){  

InputStream in=new ByteArrayInputStream(str.getBytes());

return in;  

 

}

 

 

 



 

  • 大小: 42.4 KB
  • 大小: 35.1 KB