Read utf-8 using Scanner
19,725
Solution 1
This code snippet might help:
Locale loc = new Locale("es", "ES");
Scanner sc = new Scanner(new FileInputStream(file), "UTF-8");
sc.useLocale(loc);
Solution 2
Have you tried, Scanner myScanner = new Scanner("myFile", "UTF-8");
Author by
Justin
Updated on July 22, 2022Comments
-
Justin almost 2 years
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?
-
Justin over 9 yearsSadly, it didn't. The problem probably occurs when the data is checked by setters which are called with a constructor when data is being read. As I've made data checks in my constructors, they tell me that the data is bad.
-
aviad over 9 yearsI am very bad at guessing what your code does :) would you mind sharing it and be more specific?
-
Justin over 9 yearsI was thinking how do I do this in the most efficient way and ended up uploading the whole project. I hope this helps to understand my problem: drive.google.com/…
-
Justin over 9 yearsOhh wait, I just realised I'm an idiot, since I've added data to my data file and didn't alter the code to read it correctly...