el.diablo
el.diablo

Reputation: 21

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. 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. They cannot be empty, because, even in the worst case, they would have headers.

Screenshot of the file contents in notepad++:

Output for file 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:

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)                                                                      

Upvotes: 1

Views: 147

Answers (2)

el.diablo
el.diablo

Reputation: 21

The following answer from a different post, worked.

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.

Upvotes: 1

Priyamal
Priyamal

Reputation: 2969

       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. 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

Upvotes: 1

Related Questions