🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Java 将文件读取为字符串(已针对 Java 8 更新) > 原文: [https://howtodoinjava.com/java/io/java-read-file-to-string-examples/](https://howtodoinjava.com/java/io/java-read-file-to-string-examples/) 学习**将文件读取为 Java 中的字符串**。 给定的示例使用`Files.readAllBytes()`,`Files.lines()`(以**逐行读取**)和`FileReader`和`BufferedReader`至**将文本文件读取为字符串**。 ## 将文件读取为字符串的 3 个 Java 示例 用于将文件读入字符串的所有 3 个示例的源代码。 ```java //Java 8 - Read file line by line - Files.lines(Path path, Charset cs) private static String readLineByLineJava8(String filePath) { StringBuilder contentBuilder = new StringBuilder(); try (Stream<String> stream = Files.lines( Paths.get(filePath), StandardCharsets.UTF_8)) { stream.forEach(s -> contentBuilder.append(s).append("\n")); } catch (IOException e) { e.printStackTrace(); } return contentBuilder.toString(); } //Files.readAllBytes(Path path) - Java 7 and above private static String readAllBytesJava7(String filePath) { String content = ""; try { content = new String ( Files.readAllBytes( Paths.get(filePath) ) ); } catch (IOException e) { e.printStackTrace(); } return content; } //Using BufferedReader and FileReader - Below Java 7 private static String usingBufferedReader(String filePath) { StringBuilder contentBuilder = new StringBuilder(); try (BufferedReader br = new BufferedReader(new FileReader(filePath))) { String sCurrentLine; while ((sCurrentLine = br.readLine()) != null) { contentBuilder.append(sCurrentLine).append("\n"); } } catch (IOException e) { e.printStackTrace(); } return contentBuilder.toString(); } ``` 我已将文件`data.txt`放置在位置`c:/temp`。 我将在所有 3 个示例中阅读此文件。 ```java Welcome to howtodoinjava.com blog. Learn to grow. ``` ## `Files.lines()` – 在 Java 8 中将文件读取为字符串 [`lines()`](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#lines-java.nio.file.Path-java.nio.charset.Charset-)方法**从文件中读取所有行以流**,并随着[流](//howtodoinjava.com/java8/java-8-tutorial-streams-by-examples/)被消耗而懒惰地填充。 使用指定的字符集将文件中的字节解码为字符。 ```java import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Stream; public class ReadFileToString { public static void main(String[] args) { String filePath = "c:/temp/data.txt"; System.out.println( readLineByLineJava8( filePath ) ); } //Read file content into string with - Files.lines(Path path, Charset cs) private static String readLineByLineJava8(String filePath) { StringBuilder contentBuilder = new StringBuilder(); try (Stream<String> stream = Files.lines( Paths.get(filePath), StandardCharsets.UTF_8)) { stream.forEach(s -> contentBuilder.append(s).append("\n")); } catch (IOException e) { e.printStackTrace(); } return contentBuilder.toString(); } } ``` 输出: ```java Welcome to howtodoinjava.com blog. Learn to grow. ``` ## 将整个文件读到字符串 – `Files.readAllBytes()`(≥ Java 7) [`readAllBytes()`](https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#readAllBytes(java.nio.file.Path))方法*从文件*读取所有字节。 该方法可确保在读取所有字节或引发 I/O 错误或其他运行时异常时关闭文件。 读取所有字节后,我们将这些字节传递给`String`类构造器以创建一个字符串。 ```java import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class ReadFileToString { public static void main(String[] args) { String filePath = "c:/temp/data.txt"; System.out.println( readAllBytesJava7( filePath ) ); } //Read file content into string with - Files.readAllBytes(Path path) private static String readAllBytesJava7(String filePath) { String content = ""; try { content = new String ( Files.readAllBytes( Paths.get(filePath) ) ); } catch (IOException e) { e.printStackTrace(); } return content; } } ``` 输出: ```java Welcome to howtodoinjava.com blog. Learn to grow. ``` ## 逐行读取文件 – `BufferedReader`(< Java 7) 如果仍不使用 Java 7 或更高版本,则使用[`BufferedReader`](https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html)类。 `readLine()`方法**一次读取一行文件**,然后返回内容。 ```java import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class ReadFileToString { public static void main(String[] args) { String filePath = "c:/temp/data.txt"; System.out.println( usingBufferedReader( filePath ) ); } //Read file content into string with - using BufferedReader and FileReader //You can use this if you are still not using Java 8 private static String usingBufferedReader(String filePath) { StringBuilder contentBuilder = new StringBuilder(); try (BufferedReader br = new BufferedReader(new FileReader(filePath))) { String sCurrentLine; while ((sCurrentLine = br.readLine()) != null) { contentBuilder.append(sCurrentLine).append("\n"); } } catch (IOException e) { e.printStackTrace(); } return contentBuilder.toString(); } } ``` 输出: ```java Welcome to howtodoinjava.com blog. Learn to grow. ``` 使用上述任何一种方法,都可以使用 Java 将文件**读入字符串**。 学习愉快!