Text encoding for RTF in java

Asked

Viewed 221 times

1

I’m trying to modify a file .RTF, where the parameters that will be modified are settados as numbers. However, their characters are in ANSI believe me, because the file has its header the following directives in RTF

{\rtf1\ansi\ansicpg1252

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

Usu\'e1rio
Pe\'e7a 
(N\'famero)

1 answer

1

ANSI is a normalization, in fact the code 1252 is associated with charset Windows-1252. You can associate a charset to your Bufferedreader in this way:

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

I tested with a file with encoding 1252 and the accent appeared correctly. Below the code of the test carried out:

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();
    }
}

Upshot:

Usuário
Número
  • 1

    Could you tell me how you did the test ? I tried using this function and unfortunately it didn’t work. I tried using it in files .txt and .rtf.

Browser other questions tagged

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