I’m working on a project, but when I am reading from file it can’t read some characters (like č , ž , š, etc.) I dont know what am I am doing wrong.
Here is my code:
try {
reader = new InputStreamReader(getAssets().open("koce_podatki.txt"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader br = new BufferedReader(reader);
for(int i=-1;i<position;i++){
try {
temp = "" + br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Your problem is one of encoding. Files only store bytes.
There are many ways to map bytes to characters (those ways are called encoding).
When you read from a text file, you must know and specify which encoding to use.
If you don’t specify the encoding in Java, the platform default encoding will be used, which may or may not be what you want.
In your case it is not what you want. To fix this, find out the correct encoding and specify it in the
InputStreamReaderconstructor.A common encoding to try would be UTF-8. If you told us what you see instead of those characters, we could help you guess the correct encoding.