Java IO 之 将文件读取为字符串[已针对Java 8更新]

学习使用Java 将文件读取为字符串。由于示例使用Files.readAllBytes()Files.lines()(到逐行读取)和FileReaderBufferedReader阅读文本文件的字符串

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在location c:/temp。我将在所有3个示例中阅读此文件。

Welcome to how2codex.com blog。 
Learn to grow.

Files.lines()–在Java 8中将文件读取为字符串

lines()方法从文件中读取所有行以进行 Stream 传输,并在 Stream 被消耗时延迟填充。使用指定的字符集将文件中的字节解码为字符。

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();
	}
}

输出:

欢迎来到how2codex.com博客。 
学会成长。

将整个文件读到字符串– Files.readAllBytes()[≥Java 7]

readAllBytes()方法从file读取所有字节。该方法可确保在读取所有字节或引发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;
	}
}

输出:

欢迎来到how2codex.com博客。 
学会成长。

逐行读取文件– 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();
	}
}

输出:

欢迎来到how2codex.com博客。 
学会成长。

使用上述任何一种方法,都可以使用java文件读入字符串

saigon has written 1445 articles

Leave a Reply