Java 将文件读取为字符串(已针对 Java 8 更新)
原文: https://howtodoinjava.com/java/io/java-read-file-to-string-examples/
学习将文件读取为 Java 中的字符串。 给定的示例使用Files.readAllBytes()
,Files.lines()
(以逐行读取)和FileReader
和BufferedReader
至将文本文件读取为字符串。
将文件读取为字符串的 3 个 Java 示例
用于将文件读入字符串的所有 3 个示例的源代码。
//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 个示例中阅读此文件。
Welcome to howtodoinjava.com blog.
Learn to grow.
Files.lines()
– 在 Java 8 中将文件读取为字符串
lines()
方法从文件中读取所有行以流,并随着流被消耗而懒惰地填充。 使用指定的字符集将文件中的字节解码为字符。
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();
}
}
输出:
Welcome to howtodoinjava.com blog.
Learn to grow.
将整个文件读到字符串 – Files.readAllBytes()
(≥ Java 7)
readAllBytes()
方法从文件读取所有字节。 该方法可确保在读取所有字节或引发 I/O 错误或其他运行时异常时关闭文件。
读取所有字节后,我们将这些字节传递给String
类构造器以创建一个字符串。
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;
}
}
输出:
Welcome to howtodoinjava.com blog.
Learn to grow.
逐行读取文件 – BufferedReader
(< Java 7)
如果仍不使用 Java 7 或更高版本,则使用BufferedReader
类。 readLine()
方法一次读取一行文件,然后返回内容。
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();
}
}
输出:
Welcome to howtodoinjava.com blog.
Learn to grow.
使用上述任何一种方法,都可以使用 Java 将文件读入字符串。
学习愉快!