title: Reading special characters from File - Java
body: <p>I am reading data from a text file with following properties:</p>
  
  <blockquote>
    <p>Encoding: ANSI<br>
     File Type: PC</p>
  </blockquote>
  
  <p>Now, the file contains lot of special characters like degree symbol(º) etc. I am reading this file using the following code:  </p>
  
  <pre><code>File file = new File("C:\\X\\Y\\SpecialCharacter.txt");
  BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
  </code></pre>
  
  <p>If the file encoding is ANSI, the above code does not read the special characters properly e.x. the line in file:<br>
  "Lower heat and simmer until product reaches internal temperature of 165ºF" , <code>reader.readLine()</code> would output:<br>
   "Lower heat and simmer until product reaches internal temperature of 165�F"</p>
  
  <p>When I changed the encoding for the file to UTF-8, the line reads as it is in the file without messing up the special characters.</p>
  
  <p>My question, at what point does the data get messed up? When storing the data in the file or when reading it from the file? Opening the file in Notepad displays all the special characters properly. How does that happen ?</p>
  
  <p>Hexdump output:</p>
  
  <pre><code>          -0 -1 -2 -3  -4 -5 -6 -7  -8 -9 -A -B  -C -D -E -F
  
  00000000- 4C 6F 77 65  72 20 68 65  61 74 20 61  6E 64 20 73 [Lower heat and s]
  00000001- 69 6D 6D 65  72 20 75 6E  74 69 6C 20  70 72 6F 64 [immer until prod]
  00000002- 75 63 74 20  72 65 61 63  68 65 73 20  69 6E 74 65 [uct reaches inte]
  00000003- 72 6E 61 6C  20 74 65 6D  70 65 72 61  74 75 72 65 [rnal temperature]
  00000004- 20 6F 66 20  31 36 35 BA  46                       [ of 165.F       ]
  </code></pre>