java中的scanner.Delimiter(“\ n \ n”)

时间:2015-09-28 05:45:02

标签: java split delimiter

我有一个简单的代码来理解哪个文件读取文件(input2.txt)并显示它。

import java.util.*;
import java.io.*;

public class Test {

  public static void main(String[] args )  throws Exception {
    Scanner scanner = new Scanner(new File(args[0]) );
    scanner.useDelimiter("\n\n");   
    String[] grid = scanner.next().split("\n");
    scanner.close();

    for(int row = 0; row < grid.length; row++) {
        for(int col = 0; col < grid[row].length(); col++) {
            System.out.print(grid[row].charAt(col) + " ");
        }
        System.out.println();
    }
  } 
}
  1. input.txt
  2. enter image description here

    1. java Test input.txt
    2. enter image description here

      我不明白的是

      scanner.useDelimiter("\n\n");
      

      为什么它使用两个"\n\n"而不是一个"\n"? 仅供参考,如果我使用scanner.useDelimiter("\n"); 它不能正常工作。这只是从文件中读取第一行。

5 个答案:

答案 0 :(得分:3)

您的文件包含换行符(\n),因此将分隔符设置为单个换行符会导致next()返回第一行。

由于您的文件不包含任何空白行,即没有双重换行符,next()将返回第一个标记,即整个文件内容。

至于为什么有人选择\n\n?谁知道呢。

另请注意,这不适用于Windows,其中行以\r\n分隔。

答案 1 :(得分:1)

将分隔符设置为\n\n将导致scanner.next()将整个文件作为一个字符串返回,因为找不到双重新行。将分隔符设置为从未找到的任何内容都会产生相同的行为。

接下来,使用split("\n")拆分该字符串,该数组将从文件中每行包含一个条目。

这可以像这样写得更干净

try (Scanner scanner = new Scanner(new File(args[0]))) {
    while (scanner.hasNextLine()) {
        String row = scanner.nextLine();
        for (int col = 0; col < row.length(); col++) {
            System.out.print(row.charAt(col) + " ");
        }
        System.out.println();
    }
}

答案 2 :(得分:1)

我不明白您为split使用Scanner的原因。第二个是第一个应该做的。

相反,使用\n作为Scanner的分隔符,但由于您使用的是next(),这意味着您应该迭代Scanner 直到没有更多要扫描。

split真的让你失去了使用Scanner的所有兴趣。您只需读取整个文件一次,并将其全部存储在内存中。这可能会导致大文件的内存异常。

未经测试,但这样的事情应该有效:

import java.util.*;
import java.io.*;

public class Test {

  public static void main(String[] args )  throws Exception {
    Scanner scanner = new Scanner(new File(args[0]) );
    scanner.useDelimiter("\n");   

    while (scanner.hasNext()) {
        String row = scanner.next();

        for(int col = 0; col < row.length(); col++) {
            System.out.print(row.charAt(col) + " ");
        }
        System.out.println();
    }
    scanner.close();
  } 
}

答案 3 :(得分:0)

案例1 :您使用scanner.useDelimiter("\n\n"); “\ n \ n”是在文件中找不到的分隔符,因此第一个scanner.next()将为您提供整个文件!

当你在整个文件上使用.split(“\ n”)时,它会给你一个在“\ n”分割的文件中找到的元素数组,这会让你觉得你的代码正在运行。

import java.util.*;
import java.io.*;

public class Test {

  public static void main(String[] args )  throws Exception {
    Scanner scanner = new Scanner(new File(args[0]) );
    scanner.useDelimiter("\n\n");   
    String[] grid = scanner.next().split("\n");
    scanner.close();

    for(int row = 0; row < grid.length; row++) {
        for(int col = 0; col < grid[row].length(); col++) {
            System.out.print(grid[row].charAt(col) + " ");
        }
        System.out.println();
    }
  } 
}

案例2 当您使用scanner.useDelimiter("\n")时,scanner.next()会返回第一行,因为扫描仪已找到分隔符,并逐行为您提供数据。

现在,当你使用scanner.next().split("\n");时,正则表达式“\ n”不匹配,你再次获得了第一行的整个字符串,这使你认为你的代码无效。

如果你想阅读文件,你应该在某个答案中指定的while循环中使用scanner.hasNext()

答案 4 :(得分:-1)

使用分隔符&#39; \ n \ n&#39;你告诉扫描仪在那里拆分令牌。我的假设是输入文件是一个Uninx样式文件(所以换行符是\ n),它在末尾有两个换行符或者某个地方有一个空行。

所以scanner.next()基本上会在末尾(或空行)返回一个sting标记,然后逐行分割。

因此,更好的方法可能是:

Scanner scanner = new Scanner(new File(args[0]) );
scanner.useDelimiter("\n");
List<String> grid = new ArrayList<>();
while (scanner.hasNext()) {
    grid(scanner.next());
}   
scanner.close();

您的代码只会处理文件的内容,直到有两个文件内容为