Java InputSteam

1、InputStream转化为String

1、使用 InputStreamReaderStringBuilder (JDK)

  1. public class InputStream2String {
  2. public static void main(String[] args) {
  3. try {
  4. InputStream inputStream = new FileInputStream("E:/duckAndJava/IO/testFile.txt"); //路径修改为本地文件所在的位置
  5. char[] buffer = new char[1024]; //根据需要的数组大小进行自定义
  6. StringBuilder out = new StringBuilder();
  7. Reader in = new InputStreamReader(inputStream, "UTF-8");
  8. for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) {
  9. out.append(buffer, 0, numRead);
  10. }
  11. String myString = out.toString();
  12. System.out.println("myString = " + myString);
  13. }catch (IOException e){
  14. e.printStackTrace();
  15. }
  16. }
  17. }

2、使用 inputStream.read() and StringBuilder

  1. StringBuilder sb = new StringBuilder();
  2. for (int ch; (ch = inputStream.read()) != -1; ) {
  3. sb.append((char) ch);
  4. }
  5. String myString = sb.toString();

3、使用 ByteArrayOutputStream and inputStream.read

  1. ByteArrayOutputStream result = new ByteArrayOutputStream();
  2. byte[] buffer = new byte[1024];
  3. for (int length; (length = inputStream.read(buffer)) != -1; ) {
  4. result.write(buffer, 0, length);
  5. }
  6. String myString = result.toString("UTF-8");
  1. ByteArrayOutputStream result = new ByteArrayOutputStream();
  2. byte[] buffer = new byte[1024];
  3. int length;
  4. while ((length = inputStream.read(buffer)) != -1) {
  5. result.write(buffer, 0, length);
  6. }
  7. String str = result.toString(StandardCharsets.UTF_8.name());
  8. return str;
  1. BufferedInputStream bis = new BufferedInputStream(inputStream);
  2. ByteArrayOutputStream buf = new ByteArrayOutputStream();
  3. int result = bis.read();
  4. while(result != -1) {
  5. buf.write((byte) result);
  6. result = bis.read();
  7. }
  8. String str = buf.toString();
  9. return str;

4、使用 BufferedInputStreamByteArrayOutputStream

  1. BufferedInputStream bis = new BufferedInputStream(inputStream);
  2. ByteArrayOutputStream buf = new ByteArrayOutputStream();
  3. for (int result = bis.read(); result != -1; result = bis.read()) {
  4. buf.write((byte) result);
  5. }
  6. String myString = buf.toString("UTF-8");

5、使用 BufferedReader

  1. String newLine = System.getProperty("line.separator");
  2. BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
  3. StringBuilder result = new StringBuilder();
  4. for (String line; (line = reader.readLine()) != null; ) {
  5. if (result.length() > 0) {
  6. result.append(newLine);
  7. }
  8. result.append(line);
  9. }
  10. String myString = result.toString();
  1. StringBuilder sb = new StringBuilder();
  2. String line;
  3. BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
  4. while ((line = br.readLine()) != null) {
  5. sb.append(line);
  6. }
  7. String str = sb.toString();
  8. return str;
  1. String result = new BufferedReader(new InputStreamReader(inputStream))
  2. .lines().collect(Collectors.joining(System.lineSeparator()));
  1. String result = new BufferedReader(new InputStreamReader(inputStream))
  2. .lines().parallel().collect(Collectors.joining(System.lineSeparator()));

6、使用 Stream API 或 parallel Stream API

  1. String myString = new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining("\n"));

  1. String myString = new BufferedReader(new InputStreamReader(inputStream)).lines().parallel().collect(Collectors.joining("\n"));

7、使用 StringWriterIOUtils.copy (Apache Commons)

  1. StringWriter writer = new StringWriter();
  2. IOUtils.copy(inputStream, writer, StandardCharsets.UTF_8.name());
  3. return writer.toString();

甚至可以直接这样用

  1. String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);

8、使用CharStreams(Google Guava)

  1. String result = CharStreams.toString(new InputStreamReader(inputStream, Charsets.UTF_8));
  1. //方法十二:
  2. String str = new String(ByteStreams.toByteArray(inputStream))

分别按照字符串长度来进行测试。
当使用的是一个小字符串(length=175),得到的性能测试结果如下:
2021-05-26-13-57-32-615688.png
当使用的是一个长字符串(length=50100),得到的性能测试结果如下:
2021-05-26-13-57-32-743346.png
为了更加直观,按照字符串的长度与相应函数消耗的平均时间,做了如下的表格:
2021-05-26-13-57-32-818147.png
更加直观的表格图,如下:
2021-05-26-13-57-32-897193.png

9、JDK原生提供

  1. byte[] bytes = new byte[0];
  2. bytes = new byte[inputStream.available()];
  3. inputStream.read(bytes);
  4. String str = new String(bytes);
  1. Scanner s = new Scanner(inputStream).useDelimiter("\\A");
  2. String str = s.hasNext() ? s.next() : "";
  1. String resource = new Scanner(inputStream).useDelimiter("\\Z").next();
  2. return resource;

2、String转化为InputStream

2.1 JDK原生提供

  1. InputStream is = new ByteArrayInputStream(str.getBytes());

2.2 Apache Common提供

  1. InputStream targetStream = IOUtils.toInputStream(str, StandardCharsets.UTF_8.name());

2.3 Google Guava提供

  1. InputStream targetStream =
  2. new ReaderInputStream(CharSource.wrap(str).openStream(), StandardCharsets.UTF_8.name());