java 使用扫描仪读取 utf-8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27473513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:51:51  来源:igfitidea点击:

Read utf-8 using Scanner

javaencodingutf-8java.util.scanner

提问by Justin

I am having trouble with UTF-8 encoding when using Scanner. Example two lines of my data file:

我在使用Scanner时遇到了 UTF-8 编码问题。我的数据文件的两行示例:

000001 M?lynas Tadas 63210309683 V 2003/03/17 2016/03/17 
000002 Raudonas Tomas 65505023282 V 2006/01/26 2018/01/26

Currently I am using Scannerto read the text separately instead of the whole line as this is more convenient, but it doesn't read correctly because of the encoding. I've read about using InputStreametc but I don't want to deal with messy line chopping. Is there a way to use Scannerwith UTF-8?

目前我正在使用Scanner单独读取文本而不是整行,因为这更方便,但由于编码而无法正确读取。我已经阅读了有关使用InputStream等的信息,但我不想处理凌乱的换行。有没有办法将Scanner与 UTF-8一起使用?

回答by aviad

This code snippet might help:

此代码片段可能会有所帮助:

Locale loc = new Locale("es", "ES");
Scanner sc = new Scanner(new FileInputStream(file), "UTF-8");
sc.useLocale(loc);

回答by rossum

Have you tried, Scanner myScanner = new Scanner("myFile", "UTF-8");

你有没有尝试过, Scanner myScanner = new Scanner("myFile", "UTF-8");