print special characters when generating file

Asked

Viewed 1,032 times

3

Some strings are coming out like this when generating my . txt

É fácil entrar em contato com a área 

I read another file. txt that has several sentences, I make a treatment of all the lines and separate the part that interests me, dai Gero a file . txt You’re getting off on this. has some way of solving?

    Reader lerArquivo = new FileReader(getNomeArquivo());
    BufferedReader br2 = new BufferedReader(lerArquivo);

    String linha;

    while ((linha = br2.readLine()) != null) {

    }

//to generate the file I use

FileWriter x = new FileWriter("geraArquivo.txt", false);
x.write(organizaString);
  • It is very likely that you are reading files in a certain encoding and treating in another. For example, your file is UTF-8 and the platform is in another, as FileReader uses to the encoding of the platform gives problem. See if the read content also presents problem and if yes, try to use InputStream to read. Also check which encoding your file and your platform, to see if they are not different.

2 answers

4


Instead of using FileReader, use InputStreamReader and pass the charset.

Example:

BufferedReader br2 = new BufferedReader(
                         new InputStreamReader(new FileInputStream(getNomeArquivo()), "UTF8"));

0

File arq = new File(nomeArq);

// para ler
BufferedReader leitor = new BufferedReader(new InputStreamReader(new FileInputStream(arq.getPath()), "ISO-8859-1"));

// para escrever
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(arq.getPath()), "ISO-8859-1"));
  • It would have to explain what this charset differs from the UTF8 of response?

  • Whenever possible, comment on encoding, for example why you use one or the other encoding.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.