有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java扫描仪无法读取文本文件

我有一大堆。我正在尝试读取txt文件,但对于其中许多文件,它们不会读取。那些不会读取的文本以文本前的空行开头。例如,下面抛出一个NoTouchElementException:

public static void main(String[] args) throws FileNotFoundException{
    Scanner input = new Scanner(new File("documentSets/med_doc_set/bmu409.shtml.txt"));
    System.out.println(input.next());
}

其中正在读取的文本文件以一个空行开始,然后是一些文本。我也尝试过使用输入。skip(“[\\s]*”)可跳过任何前导空格,但会引发相同的错误。有办法解决这个问题吗

编辑: 该file托管在谷歌文档上。如果下载到文本编辑器中查看,则可以看到它开头的空行


共 (3) 个答案

  1. # 1 楼答案

    扫描器读取到行尾的所有单词或数字。此时需要调用nextLine()。如果想避免出现异常,需要调用hasNextXxxx()方法之一,以确定是否可以读取该类型

  2. # 2 楼答案

    在处理输入时,Scanner类型奇怪地不一致。它接受了I/O异常——消费者应该test for these explicitly——因此它在告知读者错误方面很松懈。但在解码字符数据时,类型是严格的——编码错误的文本或使用错误的编码将导致IOException被抛出,而类型会迅速吞下

    此代码通过错误检查读取文本文件中的所有行:

      public static List<String> readAllLines(File file, Charset encoding)
          throws IOException {
        List<String> lines = new ArrayList<>();
        try (Scanner scanner = new Scanner(file, encoding.name())) {
          while (scanner.hasNextLine()) {
            lines.add(scanner.nextLine());
          }
          if (scanner.ioException() != null) {
            throw scanner.ioException();
          }
        }
        return lines;
      }
    

    此代码读取行并将解码器不理解的代码点转换为问号:

      public static List<String> readAllLinesSloppy(File file, Charset encoding)
          throws IOException {
        List<String> lines = new ArrayList<>();
        try (InputStream in = new FileInputStream(file);
            Reader reader = new InputStreamReader(in, encoding);
            Scanner scanner = new Scanner(reader)) {
          while (scanner.hasNextLine()) {
            lines.add(scanner.nextLine());
          }
          if (scanner.ioException() != null) {
            throw scanner.ioException();
          }
        }
        return lines;
      }
    

    这两种方法都要求显式地提供encoding,而不是依赖default encoding,后者通常不是Unicode(另请参见standard constants

    代码是Java 7语法,未经测试

  3. # 3 楼答案

    它以一个空行开始,而您只打印代码中的第一行,请将其更改为:

    public static void main(String[] args) throws FileNotFoundException{
        Scanner input = new Scanner(new File("documentSets/med_doc_set/bmu409.shtml.txt"));
        while(input.hasNextLine()){
            System.out.println(input.nextLine());
        }
    }