<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://howtodoinjava.com/java/io/how-to-read-data-from-inputstream-into-string-in-java/](https://howtodoinjava.com/java/io/how-to-read-data-from-inputstream-into-string-in-java/) 學習使用`BufferedReader`,`Scanner`和`IOUtils`類**將`InputStream`轉換為字符串**。 從`InputStream`讀取字符串在幾種類型的應用程序中是非常普遍的要求,在這些應用程序中,您需要從網絡流或文件系統中讀取數據并對其進行一些操作。 如果使用`java.nio`包讀取文件,則可以使用 [**Java NIO 中的 3 種讀取文件的有效方法**](//howtodoinjava.com/java-7/nio/3-ways-to-read-files-using-java-nio/ "3 ways to read files using Java NIO")。 但是,如果您仍在使用舊的 Java IO 類,或者愿意使用**將文件讀取為字符串**的任何新有效方式,那么您來對地方了。 ```java Table of Contents 1\. InputStream to String using Guava 2\. BufferedReader 3\. IOUtils 4\. java.util.Scanner ``` ![io-read-into-string](https://img.kancloud.cn/51/7d/517dcee22c77eb2ae4c2cc24f770253d_575x191.png) ## 1\. 使用 Google Guava IO 將`InputStream`轉換為`String` Guava 庫具有一些非常有用的類和方法來執行 IO 操作。 這些類隱藏了所有復雜性,否則就會暴露出來。 #### 1.1 Maven Google Guava 的 Maven 依賴關系。 ```java <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>26.0-jre</version> </dependency> ``` #### 1.2 字節源 Java 程序使用 Google Guava 庫中的`ByteSource`類將`InputStream`讀取為`String`。 ```java package com.howtodoinjava.demo; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import com.google.common.base.Charsets; import com.google.common.io.ByteSource; public class Main { public static void main(String[] args) throws Exception { InputStream inputStream = new FileInputStream(new File("C:/temp/test.txt")); ByteSource byteSource = new ByteSource() { @Override public InputStream openStream() throws IOException { return inputStream; } }; String text = byteSource.asCharSource(Charsets.UTF_8).read(); System.out.println(text); } } ``` #### 1.2 字符流 Java 程序,用于使用 Google Guava 庫中的`CharStreams`類將`InputStream`轉換為`String`。 ```java import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import com.google.common.io.CharStreams; public class Main { public static void main(String[] args) throws Exception { InputStream inputStream = new FileInputStream(new File("C:/temp/test.txt")); String text = null; try (final Reader reader = new InputStreamReader(inputStream)) { text = CharStreams.toString(reader); } System.out.println(text); } } ``` ## 2\. 使用`BufferedReader`將`InputStream`轉換為`String` 使用[`BufferedReader`](https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html)是**將文件讀入字符串**的最簡單和流行的方法。 它有助于**逐行**讀取輸入流。 ```java package com.howtodoinjava.demo.io; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class ReadStreamIntoStringUsingReader { public static void main(String[] args) throws FileNotFoundException, IOException { InputStream in = new FileInputStream(new File("C:/temp/test.txt")); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder out = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { out.append(line); } System.out.println(out.toString()); //Prints the string content read from input stream reader.close(); } } ``` ## 3\. Apache Commons `IOUtils`(最易讀) Apache commons 有一個非常有用的類[`IOUtils`](https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/IOUtils.html)將文件內容讀入`String`。 它使代碼**更清晰**,并且易于閱讀。 它也是**快速**的。 使用兩種方法之一: 1. **`IOUtils.copy()`** 2. **`IOUtils.toString()`** ```java package com.howtodoinjava.demo.io; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.StringWriter; import org.apache.commons.io.IOUtils; public class ReadStreamIntoStringUsingIOUtils { public static void main(String[] args) throws FileNotFoundException, IOException { //Method 1 IOUtils.copy() StringWriter writer = new StringWriter(); IOUtils.copy(new FileInputStream(new File("C:/temp/test.txt")), writer, "UTF-8"); String theString = writer.toString(); System.out.println(theString); //Method 2 IOUtils.toString() String theString2 = IOUtils.toString(new FileInputStream(new File("C:/temp/test.txt")), "UTF-8"); System.out.println(theString2); } } ``` ## 4\. 使用掃描器將 Java `InputStream`轉換為`String` 使用[掃描器](https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html)類不是??很流行,但是可以使用。 它起作用的原因是因為`Scanner`對流中的標記進行迭代,并且在此過程中,我們可以使用“輸入邊界的起點”(A)來分隔標記,因此僅給我們一個流的整個內容的標記。 ```java package com.howtodoinjava.demo.io; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class ReadStreamIntoStringUsingScanner { @SuppressWarnings("resource") public static void main(String[] args) throws FileNotFoundException, IOException { FileInputStream fin = new FileInputStream(new File("C:/temp/test.txt")); java.util.Scanner scanner = new java.util.Scanner(fin,"UTF-8").useDelimiter("\A"); String theString = scanner.hasNext() ? scanner.next() : ""; System.out.println(theString); scanner.close(); } } ``` 就這樣。 這篇文章的目的是為特定目的提供快速鏈接,即**將輸入流讀取到字符串**中。 [**下載源碼**](https://docs.google.com/file/d/0B7yo2HclmjI4MjI3VlR5RkpTU0U/edit?usp=sharing) 學習愉快!
                  <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>

                              哎呀哎呀视频在线观看