gpt4 book ai didi

java - 如何从java中的文件中读取一段

转载 作者:行者123 更新时间:2023-11-30 07:59:37 24 4
gpt4 key购买 nike

我得到了一个文件,里面有很多段落。我期望的输出是我一次阅读一段并对其执行操作。

final String PARAGRAPH_SPLIT_REGEX = "(?m)(?=^\\s{4})";

String currentLine;

final BufferedReader bf = new BufferedReader(new FileReader("filename"));


currentLine = bf.readLine();

final StringBuilder stringBuilder = new StringBuilder();
while(currentLine !=null) {

stringBuilder.append(currentLine);
stringBuilder.append(System.lineSeparator());
currentLine = bf.readLine();
}

String[] paragraph= new String[stringBuilder.length()];

if(stringBuilder!=null) {

final String value = stringBuilder.toString();
paragraph = value.split(PARAGRAPH_SPLIT_REGEX);
}

for (final String s : paragraph) {

System.out.println(s);
}

文件(每段前有2个字符的空格,段与段之间没有空行):

                      Story

  Her companions instrument set estimating sex remarkably solicitude motionless. Property men the why smallest graceful day insisted required. Inquiry justice country old placing sitting any ten age. Looking venture justice in evident in totally he do ability. Be is lose girl long of up give.
  "Trifling wondered unpacked ye at he. In household certainty an on tolerably smallness difficult. Many no each like up be is next neat. Put not enjoyment behaviour her supposing. At he pulled object others."
  Passage its ten led hearted removal cordial. Preference any astonished unreserved mrs. Prosperous understood middletons in conviction an uncommonly do. Supposing so be resolving breakfast am or perfectly. Is drew am hill from mr. Valley by oh twenty direct me so.
  Departure defective arranging rapturous did believing him all had supported. Family months lasted simple set nature vulgar him.   "Picture for attempt joy excited ten carried manners talking how. Suspicion neglected he resolving agreement perceived at an."

但是,我没有达到预期的输出。段落变量只包含两个值

  1. 文件的标题
  2. 文件的其余内容。

我想,我在这里尝试使用的正则表达式不起作用。我从这里收集的正则表达式。 Splitting text into paragraphs with regex JAVA

我正在使用 java8。

最佳答案

您可以使用带分隔符的Scanner 来遍历文本。例如:

Scanner scanner = new Scanner(text).useDelimiter("\n  ");
while (scanner.hasNext()) {
String paragraph = scanner.next();
System.out.println("# " + paragraph);
}

输出是:

#                       Story

# Her companions instrument set estimating sex remarkably solicitude motionless. Property men the why smallest graceful day insisted required. Inquiry justice country old placing sitting any ten age. Looking venture justice in evident in totally he do ability. Be is lose girl long of up give.
# "Trifling wondered unpacked ye at he. In household certainty an on tolerably smallness difficult. Many no each like up be is next neat. Put not enjoyment behaviour her supposing. At he pulled object others."
# Passage its ten led hearted removal cordial. Preference any astonished unreserved mrs. Prosperous understood middletons in conviction an uncommonly do. Supposing so be resolving breakfast am or perfectly. Is drew am hill from mr. Valley by oh twenty direct me so.
# Departure defective arranging rapturous did believing him all had supported. Family months lasted simple set nature vulgar him. "Picture for attempt joy excited ten carried manners talking how. Suspicion neglected he resolving agreement perceived at an."

关于java - 如何从java中的文件中读取一段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39196676/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com