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