Java InputStream转换为String
时间:2020-02-23 14:34:29 来源:igfitidea点击:
今天,我们将学习如何在Java中将InputStream转换为String。
在处理文件时,有时我们必须读取文件,然后将InputStream数据转换为String进行进一步处理。
Java InputStream转换为String
我们可以通过多种方式将InputStream转换为String。
- BufferedReader
- StringWriter
- Scanner
让我们研究一下所有这些类,以将InputStream对象转换为String。
1. BufferedReader
这是一个简单的程序,显示了使用BufferedReader的java InputStream到String的转换。
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; public class InputStreamToStringUsingBufferedReader { final static String fileName = "/Users/hyman/Downloads/file.txt"; public static void main(String[] args) throws IOException { long start = System.currentTimeMillis(); FileInputStream fileInputStream = null; BufferedReader bufferedReader = null; StringBuilder inputSB = null; try { fileInputStream = new FileInputStream(fileName); bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream, "UTF-8")); inputSB = new StringBuilder(); String line = bufferedReader.readLine(); while (line != null) { inputSB.append(line); line = bufferedReader.readLine(); if (line != null) { //add new line character inputSB.append("\n"); } } } finally { bufferedReader.close(); fileInputStream.close(); } String str = inputSB.toString(); long end = System.currentTimeMillis(); System.out.println("InputStream to String using BufferedReader = " + (end - start) + "ms"); } }
2. StringWriter
我们也可以使用StringWriter将InputStream转换为String。
请注意,它也使用了" BufferedReader",但是将数据写入StringWriter,然后轻松转换为String。
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.io.StringWriter; import java.io.Writer; public class InputStreamToStringUsingStringWriter { final static String fileName = "/Users/hyman/Downloads/file.txt"; public static void main(String args[]) throws IOException { long start = System.currentTimeMillis(); FileInputStream fileInputStream = null; Reader reader = null; Writer writer = new StringWriter(); char[] buffer = new char[1024]; try { fileInputStream = new FileInputStream(fileName); reader = new BufferedReader(new InputStreamReader(fileInputStream, "UTF-8")); int n; while ((n = reader.read(buffer)) != -1) { writer.write(buffer, 0, n); } } finally { reader.close(); writer.close(); fileInputStream.close(); } String str = writer.toString(); long end = System.currentTimeMillis(); System.out.println("InputStream to String using StringWriter = " + (end - start) + "ms"); } }
3.扫描仪
我们还可以使用Scanner类将文件读取为InputStream并转换为String。
import java.io.FileInputStream; import java.io.IOException; import java.util.Scanner; public class InputStreamToStringUsingScanner { final static String fileName = "/Users/hyman/Downloads/file.txt"; public static void main(String[] args) throws IOException { long start = System.currentTimeMillis(); FileInputStream fileInputStream = null; Scanner scanner = null; try { fileInputStream = new FileInputStream(fileName); scanner = new Scanner(fileInputStream, "UTF-8"); String str = scanner.useDelimiter("\A").next(); } finally { fileInputStream.close(); scanner.close(); } long end = System.currentTimeMillis(); System.out.println("InputStream to String using Scanner = "+(end-start)+"ms"); } }