<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 如何在 Java 中將`InputStream`轉換為字符串 > 原文: [https://beginnersbook.com/2013/12/how-to-convert-inputstream-to-string-in-java/](https://beginnersbook.com/2013/12/how-to-convert-inputstream-to-string-in-java/) 以下是如何讀取`InputStream`并將其轉換為`String`的完整示例。涉及的步驟是: 1)我使用[`getBytes()`方法](https://beginnersbook.com/2013/12/java-string-getbytes-method-example/) 將文件內容轉換為字節之后,使用包含內部緩沖區的`ByteArrayInputStream`初始化`InputStream`,緩沖區包含可以從流中讀取的字節。 2)使用`InputStreamReader`讀取`InputStream` 。 3)使用`BufferedReader`讀取`InputStreamReader`。 4)將`BufferedReader`讀取的每一行附加到`StringBuilder`對象上。 5)最后使用`toString()`方法將`StringBuilder`轉換為`String`。 ```java import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class Example { public static void main(String[] args) throws IOException { InputStreamReader isr = null; BufferedReader br = null; InputStream is = new ByteArrayInputStream("This is the content of my file".getBytes()); StringBuilder sb = new StringBuilder(); String content; try { isr = new InputStreamReader(is); br = new BufferedReader(isr); while ((content = br.readLine()) != null) { sb.append(content); } } catch (IOException ioe) { System.out.println("IO Exception occurred"); ioe.printStackTrace(); } finally { isr.close(); br.close(); } String mystring = sb.toString(); System.out.println(mystring); } } ``` 輸出: ```java This is the content of my file ``` #### 參考: * [`BufferedReader`](https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html) * [`ByteArrayInputStream`](https://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayInputStream.html) * [`InputStream`](https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html) * [`InputStreamReader`](https://docs.oracle.com/javase/7/docs/api/java/io/InputStreamReader.html)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看