I would like to read and print the text file to console so i did this with below code
File file = new File("G:\\text.txt");
FileReader fileReader = new FileReader(file);
int ascii = fileReader.read();
while (ascii != -1)
{
result = result + (char) ascii;
ascii = fileReader.read();
}
System.out.println(result);
although i got correct result, but in some cases i will get some strange result. Suppose my text file has this text in it:
Hello to every one
In order to have a text file I've used notepad, and when i change the encoding mode i will get strange output from my code.
Ansi : Hello to every one
Unicode : ÿþh e l l o t o e v e r y o n e
Unicode big endian: þÿ h e l l o t o e v e r y o n e
UTF-8 : hello to every one
Why do i get these strange output? Is there any problem with my code? Or there are other reasons