Java character set / character encoding
•
Java
I have a Spanish file, so it is full of the following characters:
á é í ó ú ñ Ñ Á É Í Ó Ú
I have to read the document, so I do this:
fr = new FileReader(ficheroEnTrada); BufferedReader rEnTrada = new BufferedReader(fr); String linea = rEnTrada.readLine(); if (linea == null) { logger.error("ERROR: Empty file."); return null; } String delimitador = "[;]"; String[] tokens = null; List<String> token = new ArrayList<String>(); while ((linea = rEnTrada.readLine()) != null) { // Some parsing specific to my file. tokens = linea.split(delimitador); token.add(tokens[0]); token.add(tokens[1]); } logger.info("List of tokens: " + token); return token;
When I read the token list, all special characters disappear and are replaced by this character:
Ó = Ó Ñ = Ñ
Wait
What happened? I have never encountered the charsets problem (I assume it is the charset problem) Is it because of this computer? What can I do?
Any additional suggestions will be appreciated and I am learning! thank you!
Solution
You need to specify the relevant character encoding
BufferedReader rEnTrada = new BufferedReader( new InputStreamReader(new FileInputStream(fr),"UTF-8"));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码