Encoding problems on the eclipse console

Asked

Viewed 371 times

1

When reading the contents of a file to print the same on the eclipse console, I have a sharp word and it is coming out like this on the console:

é um teste

The correct thing to show is:

é um teste

I checked the eclipse Run options and mine encoding is ISO-8859-1

How can I solve?

For testing, I’m using the following code to do the action:

final Scanner input = new Scanner(new File("/home/douglas/teste.txt"));

        while (input.hasNextLine())
        {
           System.out.println(input.nextLine());
        }
  • 1

    Got how you put in UTF-8?

  • It worked @Victorstafusa

1 answer

2


You can specify the correct encoding on builder of the Scanner:

final Scanner input = new Scanner(new File("/home/douglas/teste.txt"), "ISO-8859-1");

while (input.hasNextLine()) {
   System.out.println(input.nextLine());
}

However, I always recommend using the encoding UTF-8 which in theory would work with any characters anywhere in the world as long as everything is encoded in it. ISO-8859-1 and others encoding similar features have more regional characteristics, thus creating portability problems.

When the input file is in UTF-8, you would then do so:

final Scanner input = new Scanner(new File("/home/douglas/teste.txt"), "UTF-8");

The builder without the encoding/charset depends on the standard coding of the platform, and because of this, suffers from portability problems like this.

  • Show, thank you very much.

Browser other questions tagged

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