如何读取txt文件并保存为java中的单个字符串以便按特定字符拆分

How to read txt file and save as a single string in java in order to split it by a certain character

我是 java 语言的新手,我发现很难找到:

1. read a txt file
2. save entire txt file as single string
3. once I have the txt file content as a string, break the string (by a certain character such as a new line) into an array of strings that I can work with.

如果您可以包括所需的 import 语句,那将对 java 新程序员很有帮助。

我正在使用 BufferedReader,但我相信它只会给我当前可用的行。

我确实看到 BufferedReader 有一个 lines() 方法,其中 returns 一个 Stream<String>.

Files#lines returns 文件中所有行的流。您可以将这些行连接到一个字符串,然后根据分隔符将其拆分:

String separator = ":"; // for example...
String entireFile = 
    Files.lines(Paths.get("file.txt")).collect(Collectors.joining());
String[] separated = entireFile.split(separator);