Search code examples
javastringfilebufferedreaderfilereader

JAVA Unrecognized Character of the first character in the first line


I have lines of code to read the content of the file in Java. Basically I am using FileReader and BufferedReader. I am reading the lines correctly, however, the first character of the first line seems to be an undefined symbol. I have no idea where I got this symbol since the content of the input file is correct.

Here is the code:

FileReader readFile = new FileReader(chosenFile);
BufferedReader input = new BufferedReader(readFile);
while((line = input.readLine()) != null) {
    System.out.println(line); 
}

Console Output

File Content


Solution

  • If it apears only in the first line, this is probably BOM (Byte Order Mark). All modern Text editors recognize this and do not present it as part of the text file. When you save the text file, there should be option to save with or without it.

    If you wish to read the BOM marker in java, see here Reading UTF-8 - BOM marker