Raúl Puro Raúl Puro - 1 year ago 169
Java Question

Java replace ascii char

I have a file (prueba.txt) and I would like to replace ascii characters 0xE1 (á) for 0x14, 0xE9 (é) for 0x15, 0xF3 (ó) for 0x16 ... In string is possible with

but it is a char.

import java.io.File;
import java.util.Scanner;

public class Reemplazar {

public static void main(String[] args) throws Exception {

Scanner archivo = new Scanner(new File("prueba.txt"));

while(archivo.hasNextLine()) {
String frase = archivo.nextLine();

for (int i = 0; i < frase.length(); i++) {
char current = frase.charAt(i);

if (current == 0xe1) {
System.out.println("contiene la á: '"+frase+"'");

if (current == 0xe9) {
System.out.println("contiene es la é: '"+frase+"'");


I guess this code is much improved, but ...


Answer Source

First read the text file, then replace the characters.


A text file has some specific character set and encoding. You must know exactly what it is or that is definitely the system default ANSI character set and encoding. ANSI is not one specific encoding.

But, since you said ANSI, you probably meant the system default. The Scanner construct you used is for Java's default. You can reasonably assume that Java's default correctly matches the system default.

Replacing characters

All "characters" in Java's String, char and Character datatypes and in an analyzed Java source file are UTF-16 code units, one or two of which encode a Unicode codepoint. Unescaped literal strings and characters are going to be in the encoding of the source file. (Of course, that should be UTF-8.) Regardless, if you type it, see it, save it and compile it with the same encoding, the characters will be what you think they are.

So, once you have text in a string, you can replace, replace, replace, like this:

frase.Replace("á", "►").Replace("é","☼").Replace("ñ", "◄")


frase.Replace("\u00E1", "\u25B6")…

BTW—0x14, 0x15, 0x16 are the encodings for ►, ☼, ◄ in the one encoding for the OEM437 character set.

If you'd rather iterate through the elements of the String, you could do it by each UTF-16 code unit, such as using charAt. That would work best if all your text was characters that UTF-16 encodes with just one code unit. Given that your file encoding is one of the ANSI character sets for a European language, that is likely the case. Or, you can iterate with a codepoint-aware technique as seen in the Java documentation on CharSequence.