Question

I'm trying to write a method that reads in a file given the path and name of the file. I have called this method once before in my program, and it ran fine. However, on my second call, it's giving me an error that reads NoSuchElementException: No line found. I looked through many other threads and found that the issue was that closing the Scanner also closes System.in. However, I'm not closing any scanners, or any input/output streams. The method looks like this:

public static Map readFileMap(String path, String fileName)
    throws IOException
  {
    path = path.replace("/", "\\");
    String key = null;
    String val = null;
    Map<String, String> map = new HashMap<String,String>();
    input = new Scanner(new File(path + fileName));
    int numLines = Helper.numLines(path + fileName);
    System.out.println(numLines);
    for (int loop = 0; loop < numLines; loop++)
    //while(input.hasNextLine())
    {
      String all = input.nextLine();
      System.out.println(all);
      String[] pieces = all.split("\\s", 2);
      key = pieces[0];
      val = pieces[1];
      map.put(key, val);
    }
    return map;
  }

The variable numLines contains the (correct) value of 143. In case it has something to do with the file's data, the data looks like this:

! %21
" %22
# %23
$ %24
% %25
& %26
' %27
( %28
) %29
* %2A
+ %2B
, %2C
- %2D
. %2E
/ %2F
[ %5B
\ %5C
] %5D
^ %5E
_ %5F
` %60
~ %7E
  %7F
` %80
‚ %82
ƒ %83
„ %84
… %85
† %86
‡ %87
ˆ %88
‰ %89
Š %8A
‹ %8B
Π%8C
Ž %8E
‘ %91
’ %92
“ %93
” %94
• %95
– %96
— %97
˜ %98
™ %99
š %9A
› %9B
œ %9C
ž %9E
Ÿ %9F
¡ %A1
¢ %A2
£ %A3
¤ %A4
¥ %A5
¦ %A6
§ %A7
¨ %A8
© %A9
ª %AA
« %AB
¬ %AC
® %AE
¯ %AF
° %B0
± %B1
² %B2
³ %B3
´ %B4
µ %B5
¶ %B6
· %B7
¸ %B8
¹ %B9
º %BA
» %BB
¼ %BC
½ %BD
¾ %BE
¿ %BF
À %C0
Á %C1
 %C2
à %C3
Ä %C4
Å %C5
Æ %C6
Ç %C7
È %C8
É %C9
Ê %CA
Ë %CB
Ì %CC
Í %CD
Î %CE
Ï %CF
Ð %D0
Ñ %D1
Ò %D2
Ó %D3
Ô %D4
Õ %D5
Ö %D6
× %D7
Ø %D8
Ù %D9
Ú %DA
Û %DB
Ü %DC
Ý %DD
Þ %DE
ß %DF
à %E0
á %E1
â %E2
ã %E3
ä %E4
å %E5
æ %E6
ç %E7
è %E8
é %E9
ê %EA
ë %EB
ì %EC
í %ED
î %EE
ï %EF
ð %F0
ñ %F1
ò %F2
ó %F3
ô %F4
õ %F5
ö %F6
÷ %F7
ø %F8
ù %F9
ú %FA
û %FB
ü %FC
ý %FD
þ %FE
ÿ %FF

As I mentioned before, when I called the method the first time around, everything ran correctly. So my guess is that it has to do with me re-initializing the variable input. Any help would be greatly appreciated!

Était-ce utile?

La solution

Due to the nature of the file you are reading you should also specify the character encoding to the Scanner. For example, if the file was saved in UTF-8 you should use the constructor new Scanner(myFile, "UTF-8"). Trying out your code with this change I am able to read the file you posted without a problem (I had saved this file in UTF-8, it is possible that you are saving it in some other encoding). Also, the input.hasNextLine() call works as expected after this change.

edit : you can try using something other than Scanner, for example if you have Java 7 you can just do :

path = path.replace("/", "\\");
Map<String, String> map = new HashMap<String, String>();

for (String line : Files.readAllLines(Paths.get(path + fileName), Charset.defaultCharset())) {
  String[] pieces = line.split("\\s", 2);
  map.put(pieces[0], pieces[1]);
}

return map;

This should throw an exception if it find an unreadable character set. If you do not have Java 7 then you can try something like below instead. I've found that this method does not throw any exceptions if it has the wrong encoding, but just prints out a lot of "?" for the characters it could not read.

path = path.replace("/", "\\");
Map<String, String> map = new HashMap<String, String>();
BufferedReader reader = new BufferedReader(new InputStreamReader(
    new FileInputStream(path + fileName)));

while (true) {
  String line = reader.readLine();
  if (line == null) {
    break;
  }
  String[] pieces = line.split("\\s", 2);
  map.put(pieces[0], pieces[1]);
}

reader.close();
return map;

this

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top