Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在读取文件时,我可以使用EOF或EOL以外的其他dilimeter吗_Java - Fatal编程技术网

Java 在读取文件时,我可以使用EOF或EOL以外的其他dilimeter吗

Java 在读取文件时,我可以使用EOF或EOL以外的其他dilimeter吗,java,Java,我想解析一个文件并使用2“新行”(\n)作为dilimiter而不是EOL,然后将其添加到string对象中 这是算法- 1. While end of file is not reached 2. Read a file until you hit 2 \n's or eof is reached put that contents as a string to a List object. 3. go to 1. 你可以使用这个方法 将此扫描仪的定界图案设置为从 指定的字符串 新线是

我想解析一个文件并使用2“新行”(\n)作为dilimiter而不是EOL,然后将其添加到string对象中

这是算法-

1. While end of file is not reached
2. Read a file until you hit 2 \n's or eof is reached
  put that contents as a string to a List object. 
3. go to 1.
你可以使用这个方法

将此扫描仪的定界图案设置为从 指定的字符串

新线是

线路终止符:

the ASCII LF character, also known as "newline"

the ASCII CR character, also known as "return"

the ASCII CR character followed by the ASCII LF character

您可以使用任何您想要的分隔符。如果你愿意的话

注意:新行通常是
(\r\n |\r |\n)
,以支持新行的三种变体

在您的情况下,最简单的解决方案可能是

String[] paragraphs = FileUtils.readFileAsString(filename)
                               .split("(\r\n|\r|\n){2}");
String[] paragraphs = FileUtils.readFileAsString(filename)
                               .split("(\r\n|\r|\n){2}");