Java 把 InputStream 转换成 String 的几种方法

      我们在 Java 中经常会碰到如何把 InputStream 转换成 String 的情形,比如从文件或网络得到一个 InputStream,需要转换成字符串输出或赋给别的变量。

未真正关注这个问题之前我常用的办法就是按字节一次次读到缓冲区,或是建立 BufferedReader 逐行读取。其实大可不必费此周折,我们可以用 Apache commons IOUtils,或者是 JDK 1.5 后的 Scanner,还可用 Google  Guava 库的 CharStreams。到了 JDK7,若要从文件中直接得到字符串还能用 java.nio.file.Files#readAllLines 和 java.nio.file.Files#readAllBytes 方法。

下面看各个例子,为能够实际用运,例子写在 main 方法里,并从文件获得一个 InputStream,代码中把可能要捕获的异常抛出来。再就是注意处理输入输出流时有涉及到字符集,字符集乱了就乱码了,默认字符集是 System.getProperty("file.encoding"),通常我们都用 UTF-8,异常 UnsupportedEncodingException 继承自 IOException。

关于字符串乱码问题:http://blog.csdn.net/hyde82/article/details/394768

1. 使用 JDK 5 的 Scanner 法

package cc.unmi.test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Scanner;


public class Test {

    /**
     * @param args
     * @throws FileNotFoundException 
     */
    public static void main(String[] args) throws FileNotFoundException {
        InputStream inputStream = new FileInputStream("d:/sample.txt");
        Scanner scanner = new Scanner(inputStream, "UTF-8");
        String text = scanner.useDelimiter("\A").next();
        System.out.println(text);
        scanner.close();
    }
}

2. JDK1.4 及之前的 BufferedReader 法

package cc.unmi.test;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


public class Test {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        InputStream inputStream = new FileInputStream("d:/sample.txt");
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"utf8")); 
        String line = null; 
        while((line = bufferedReader.readLine()) != null)
        {
         
              stringBuilder.append(System.getProperty("line.separator")); /r/n
        
              stringBuilder.append(line); 
         } 
         System.out.println(stringBuilder.toString()); 
        }
 }

中间那些判断是不是第一行来决定是否加换行符

JDK1.4 及之前的 readBytes 法

package cc.unmi.test;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;


public class Test {

    /**
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        InputStream inputStream = new FileInputStream("d:/sample.txt");
        
        byte[] buffer = new byte[2048];
        int readBytes = 0;
        StringBuilder stringBuilder = new StringBuilder();
        while((readBytes = inputStream.read(buffer)) > 0){
            stringBuilder.append(new String(buffer, 0, readBytes));
        }
        
        System.out.println(stringBuilder.toString());
    }
}

缓冲区的大小自己根据实际来调,比 BufferedReader 还简洁些,不需管换行符的事情。

4. Apache commons IOUtils.toString 法

package cc.unmi.test;
import java.io.*;
import org.apache.commons.io.IOUtils;


public class Test {

    /**
     * @throws IOException 引入apache的io包
*/ public static void main(String[] args) throws IOException { 


     InputStream inputStream = new FileInputStream("d:/sample.txt");
     String text = IOUtils.toString(inputStream,"UTF-8"); 
     System.out.println(text); 
   } 
}

第三方库就是第三方库,人家充分考虑到了你的感受,你对 JDK 库的抱怨,多简洁,一行搞定。IOUtils 还能把内容拷入其他的 Writer 中,如 IOUtils.copy(inputStream, new StringWriter())。 

5. Google guava 的  CharStreams 法

package cc.unmi.test;
import java.io.*;
import com.google.common.io.CharStreams;


public class Test {

    /**
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        InputStream inputStream = new FileInputStream("d:/sample.txt");
        String text = CharStreams.toString(new InputStreamReader(inputStream, "UTF-8"));
        System.out.println(text);
    }
}

CharSteams 不是直接作用在 InputSteam 上的,还要靠 InputStreamReader 拱个桥。

6.JDK 7 的 NIO readAllBytes 

package cc.unmi.test;

import java.io.IOException;
import java.nio.file.*;


public class Test {

    /**
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        byte[] bytes = Files.readAllBytes(Paths.get("d:/sample.txt"));
        //String text = new String(bytes);
String content = new String(bytes,"gbk");//解决中文乱码 System.out.println(text); } }

7.借助 ByteArrayOutputStream  法

 InputStream inputStream = new FileInputStream("./outfile");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int i = -1;
            while ((i = inputStream.read()) != -1) {
                baos.write(i);
            }
            System.out.println(baos.toString());

8.JDK 7 的 NIO readAllBytes

byte[] bytes = Files.readAllBytes(Paths.get("d:/sample.txt"));
        String text = new String(bytes);
        System.out.println(text);

提供一个方法读取:

/**
	 * 通过文件名获取内容【解决中文乱码】
	 * @param fileName
	 * @return
	 */
	public static String getFileContent(String fileName) {
		StringBuffer stringBuffer = new StringBuffer();
		File file = new File(fileName);
		InputStream inputStream = null; // 文件输入流
		try {
			inputStream = new FileInputStream(file);

			InputStreamReader reader = new InputStreamReader(inputStream, "GBK"); // 最后的"GBK"根据文件属性而定,如果不行,改成"UTF-8"
			BufferedReader br = new BufferedReader(reader);
			//BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"utf8")); 
			String line;
			while ((line = br.readLine()) != null) {
				stringBuffer.append(line);
			}
			br.close();
			reader.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return stringBuffer.toString();

	}

  

关于乱码问题见  对Java字符类型的深入了解(转贴)