Java读取行,直到到达顶部

Java读取行,直到到达顶部,java,java.util.scanner,Java,Java.util.scanner,实际上,我正在尝试读取包含多行的文件。为此,我使用scanner.nextline() 但是,我想一直读到followstop(点分隔符),它通常后跟空格或行尾字符 在这种情况下,有人能帮我吗 使用read()方法并逐字符读取。如果你是匹配的。这是你的换行符 另一种解决方案可能是设置换行符,然后使用readline()。我怎么也没试过 或者使用string.split方法一次性读取文件如果要搜索到某个时段,可以使用带有模式的匹配器 FileReader fin = new FileReader(

实际上,我正在尝试读取包含多行的文件。为此,我使用scanner.nextline()

但是,我想一直读到followstop(点分隔符),它通常后跟空格或行尾字符

在这种情况下,有人能帮我吗

使用read()方法并逐字符读取。如果你是匹配的。这是你的换行符

另一种解决方案可能是设置换行符,然后使用readline()。我怎么也没试过


或者使用string.split方法一次性读取文件

如果要搜索到某个时段,可以使用带有
模式的
匹配器

FileReader fin = new FileReader("yourfile.txt");
Scanner src = new Scanner(fin);
// Set delimiters to full stop

src.useDelimiter(".");


while (src.hasNext()) {
  // do what you want here
  } 
//Pattern p = Pattern.compile("[^\\.]*\\.(\\s+)"); 
Pattern p = Pattern.compile(".*?\\.(\\s+)");  //Anything any amount of times, 
                                              //followed by a dot and then some whitespace.

Matcher matcher = p.matcher("firstword. secondword.\n");

while(matcher.find()){
    boolean space = matcher.group(1).charAt(0) == ' ';
    System.out.println(matcher.start() + matcher.group() + "and is space: " + (space ? "TRUE" : "FALSE"));
}
  • *?
    -
    将匹配任何内容<代码>*
  • 匹配0次或更多次<代码>?是匹配器。这将匹配任何类型的任意数量的字符,但它会在第一个句点和空格之前停止(因为使用了lazy运算符)
  • \\.
    -这与句点匹配。在Java中,必须对正则表达式中的特殊字符进行双转义
  • (\\s+
    )-这意味着一次或多次匹配空白(
    \s
    ,包括新行)。它匹配一个或多个空白字符。括号“捕获”了正则表达式的这一部分,因此每次在正则表达式上找到匹配项时,您都可以问它括号内匹配了哪些特定部分。这让您知道它是空格还是换行符
  • matcher.group()
    获取刚刚匹配的字符串

    我在问号中加了一条,注释了另一种模式,因为它听起来像是在一些数据中间有一段时间。问号进行“惰性”匹配。默认情况下,匹配是贪婪的,将使用最长的匹配字符串。因此,如果字符串中有多个位置后跟一个句点和一个空格,那么它会将所有这些作为一个匹配返回。惰性迫使它在到达第一个句点和空格后停止匹配任何字符(.*)

    试试这个

            StringBuilder stringBuilder = new StringBuilder();
            while ((line = bufferedReader.readLine()) != null)
            {
                if (line.contains(". ") || line.trim().endsWith("."))
                {
                    int length = line.indexOf(". "); // get the index when the line contains dot and space in the middle
                    stringBuilder.append(line.trim().endsWith(".") ? line
                            : line.substring(0, length).replace(". ", "." + System.getProperty("line.separator"))); // when the line contains dot at the end or the line may contain the dot with space
                    System.out.println("stringBuilder : " + stringBuilder.toString());
                    stringBuilder.delete(0, stringBuilder.toString().length());
                    if (length != 0)
                    {
                        stringBuilder.append(line.substring(length+2, line.length()));
                    }
                }
                else
                {
                    stringBuilder.append(line.replace(System.getProperty("line.separator"), " "));
                }
            }
            System.out.println("stringBuilder : "+stringBuilder.toString()); // when the last line not end with dot or not contain dot and space
    

    你试过什么?把代码给我们,这样你就更容易得到帮助谢谢你的回复。。。。事实上,我不明白这个模式,如果我想读到点后跟(空格或换行符)Ok。我会在里面把它分解得更细。我为空白添加了\s。谢谢,我会尝试,看看会发生什么。。。。但是如何同时处理新行字符(\n)和空格新行字符是空格。您可以检查哪一个是字符串的最后一个字符,也可以在末尾匹配一个组。一旦我弄明白为什么正则表达式的空白部分不起作用,我就会把它加进去。非常感谢。。。。我尝试了您的代码,并在您的示例中获得了成功。。。。但是当我在我的程序中使用它时,如下scanner=newscanner(文件);scanner.useDelimiter(“.*?\\(\\s+)”;我试着使用这个方法,但是如果我有其他的东西,例如23.5,它会认为点是一个线分离器。我会尽我最大的努力来运行你发送给我的东西,我会看到会发生什么。非常感谢,为什么不尝试在“.”之后添加一个空格,使其变成“.”如下:src.useDelimeter(“.”);你知道这有700个额外的闭合括号和一个没有if的else,对吧?不是故意的,但它确实需要一些格式。