Minerbob Minerbob - 22 days ago 6
Java Question

Java Scanner Class bad character "®"

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 excepted?

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

}
}

Answer

Mention the encoding when you create the scanner.

Scanner file= new Scanner(new File(fileName), "utf-8");