Java Scanner Class bad character "®"

Minerbob :

I have a scanner class reading a file into a string. Any file with this character "®" causes it to fail. I'm new to Java, Is there a better way to read this file so that character would be accepted?

public void readFile(String fileName)
{
    fileText = "";

    try
    {
        Scanner file = new Scanner(new File(fileName));
        while (file.hasNextLine())
        {
            String line = file.nextLine();
            fileText += line +"\r"+"\n";
        }
        file.close();
    }
    catch (Exception e)
    {
         System.out.println(e);

   }
      }
Adam :

By default Scanner uses the platform default character encoding, this might not match the character encoding of the file. JavaDoc states:

Constructs a new Scanner that produces values scanned from the specified file. Bytes from the file are converted into characters using the underlying platform's default charset.

First determine what character encoding your file is in, this can be done with the Linux command line utility file -i. Pass the correct encoding into the scanner. Java 7 contains predefined constants in java.nio.charset.StandardCharsets for some well known character sets.

Scanner file = new Scanner(new File(fileName), StandardCharsets.UTF_8);

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=422496&siteId=1