Curly quotes causing Java Scanner hasNextLine() to be false — why?

前端 未结 3 1800
遥遥无期
遥遥无期 2021-01-17 12:22

I\'ve been having an issue getting the java.util.Scanner to read a text file I saved in Notepad, even though it works fine with others. Basically, when it tries to read the

3条回答
  •  鱼传尺愫
    2021-01-17 12:24

    If you don't specify an encoding when you create the scanner it will try to divine the encoding based on a byte order mark (BOM), which is the first few bytes of a file. If it doesn't have one, it will default to whatever default the OS uses. Since you're using Windows, the default is cp-1252. It seems that notepad is saving your text file using ISO-8859-1 which is similar, but not that same as cp-1252. See this link for more details:

    http://www.i18nqa.com/debug/table-iso8859-1-vs-windows-1252.html

    When you save it as UTF-8, it probably places the UTF-8 BOM at the beginning of the file and the scanner can pick up on it.

    If you want to look more into BOM, look it up in wikipedia--the article is quite good. You can also download PSPad and open the text file in hex mode to see the individual bytes. Hope that helps :)

提交回复
热议问题