I have a method which loads file from sdcard (Android) and then reads it with StringBuilder. The text which im reading is written with my native language characters such as ą ś ć ź ż... StringBuilder (or FileInputStream) can't read them properly unfortunately. How I can set proper encoding ?
here is the code :
File file = new File(filePath);
FileInputStream fis = null;
StringBuilder builder = new StringBuilder();
try {
fis = new FileInputStream(file);
int content;
while ((content = fis.read()) != -1) {
builder.append((char) content);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
System.out.println("File Contents = " + builder.toString());
contactService.updateContacts(builder.toString());
for example you could try an InputStreamReader combinded with a BufferedReader, that should do the trick:
InputStreamReader inputStreamReader = new InputStreamReader((InputStream)fis, "UTF-8");
BufferedReader br = new BufferedReader(inputStreamReader);
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);
}
So long, Tom