Read utf-8 using Scanner [closed]

ぃ、小莉子 提交于 2019-12-01 03:26:21

问题


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

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 Scanner to 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 InputStream etc but I don't want to deal with messy line chopping. Is there a way to use Scanner with UTF-8?


回答1:


This code snippet might help:

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



回答2:


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



来源:https://stackoverflow.com/questions/27473513/read-utf-8-using-scanner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!