简体   繁体   English

使用扫描仪读取文件时出现问题

[英]Issue in reading a file with Scanner

This is a quite simple task and I have done this a lot of times. 这是一个非常简单的任务,我已经做了很多次。 But, at the moment, I am stuck at this trivial line of code. 但是,此刻,我陷入了这一琐碎的代码行中。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Test {

private static Scanner scan;

  public static void main(String[] args) throws FileNotFoundException {
    // TODO Auto-generated method stub
    File file = null;
    switch (1) {
    case 1:
        file = new File("W:\\Umesoft Evobus\\From AQUA\\Aqua data_ All\\20090101-20090630_datenabzug_tilde.txt");
        break;
    case 2:
        file = new File("W:\\Umesoft Evobus\\From AQUA\\Aqua data_ All\\20090701-20091231_datenabzug_tilde.txt");
        break;
    }
    scan = new Scanner(file);
    String x = scan.nextLine();
    System.out.println(x);
  }

}

When I try to read the first file, I get a NoSuchElementException. 当我尝试读取第一个文件时,出现NoSuchElementException。 When I try to read the second file, I have no issues. 当我尝试读取第二个文件时,没有任何问题。 Both the files are from the same source and have the same format. 这两个文件来自相同的来源,并且具有相同的格式。 I am sure, there are no issues with regards to the input files. 我敢肯定,关于输入文件没有问题。 The first line in both the files are identical. 这两个文件的第一行相同。

Can someone explain this situation? 有人可以解释这种情况吗?

The above program is just for testing. 上面的程序仅供测试。 Hence, I have used a switch case to select the file. 因此,我使用了一个开关盒来选择文件。

In the actual program, a set of files are selected by the user. 在实际程序中,用户选择了一组文件。 And every time, this file is being skipped. 而且每次都会跳过此文件。 The input files are data files, generated through another program. 输入文件是通过另一个程序生成的数据文件。 They are very similar to CSV files, but the delimiter used here is ~ for some reasons. 它们与CSV文件非常相似,但是出于某些原因,此处使用的定界符为〜。 They cannot be empty, because, even in the worst case, they would have headers. 它们不能为空,因为即使在最坏的情况下,它们也将具有标题。

Screenshot of the file contents in notepad++: notepad ++中文件内容的屏幕快照:

Output for file 1: 文件1的输出:

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at controller.Test.main(Test.java:24)

Output for file 2: 文件2的输出:

Weltherstellercode~FIN~Fahrzeug_Baumuster~~Motoridentnummer~Getriebe_Identifizierungsnummer~Produktionsdatum~Produktionsnummer_Fzg~Erstzulassungsdatum~Reparaturdatum~Fahrzeug_Laufleistung_in_km~Interne_VEGA_Antragsnummer~TGA~Fehlerort~~Fehlerart~~Reparaturart~~Hauptschadensteil~Reparaturland_(G&K)~~Reparaturbetrieb_(G&K)~~Mitteilungstext~Gutschriftsdatum_(Summe)~Anzahl_Beanstandungen~Gesamtkosten~Lohnkosten~Materialkosten~Summe_DH+NK~Anzahl_Arbeitswerte_(Gutgeschrieben)                                                                      
       String line ="";
       BufferedReader br = new BufferedReader(new FileReader("path"));
       while ((line = br.readLine()) != null) {
       System.out.println(line);            
       }

i changed previous code to use a buffered reader since 我将以前的代码更改为使用缓冲读取器,因为

BufferedReader has significantly larger buffer memory than Scanner. BufferedReader的缓冲区内存比Scanner大得多。 Use BufferedReader if you want to get long strings from a stream, and use Scanner if you want to parse specific type of token from a stream 如果要从流中获取长字符串,请使用BufferedReader;如果要从流中解析特定类型的令牌,请使用Scanner。

The following answer from a different post, worked. 来自不同帖子的以下答案有效。

https://stackoverflow.com/a/35173548/6234625 https://stackoverflow.com/a/35173548/6234625

scan = new Scanner(file,"UTF-8");

I had to mention the encoding for the Scanner. 我不得不提到扫描仪的编码。

Thanks to everyone who tried to help me. 感谢所有试图帮助我的人。 Thanks especially to @Abhisheik and @Priyamal. 特别感谢@Abhisheik和@Priyamal。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM