Text encoding for RTF in java

1

I'm trying to modify a .RTF file, where the parameters to be modified are set as numbers. However, its characters are in ANSI I believe, because the file has its header the following directives in RTF

{\rtf1\ansi\ansicpg1252

How do I write in the file and the words contain accents, how can I encode my string so that it returns something read by RTF ???. Below is an example of the encoding used in the

Usu\'e1rio
Pe\'e7a 
(N\'famero)
    
asked by anonymous 24.09.2018 / 23:14

1 answer

1

ANSI is a normalization, actually code 1252 is associated with the Windows-1252 charset. You can associate a charset to your BufferedReader like this:

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("caminho para o arquivo"), "Windows-1252"));

I tested with a 1252 encoding file and the accent appeared correctly. Here is the test code:

File (Encoded in Windows-1252):

Usu�rio 
N�mero

Code:

public static void main(String[] args) {
    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("src/teste.txt"), "Windows-1252"));

        String linha;
        while((linha = br.readLine()) != null) {
            System.out.println(linha);
        }

        br.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Result:

Usuário
Número
    
25.09.2018 / 01:03