Read source file with Unicode characters
I have a file in the / res / raw folder (R.raw.test) with the following content:
This is Tésêt
I want to read it in line. My current code:
public static String readRawTextFile(Context ctx, int resId) {
InputStream inputStream = ctx.getResources().openRawResource(resId);
InputStreamReader inputreader;
try {
inputreader = new InputStreamReader(inputStream, "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
return null;
}
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
StringBuilder text = new StringBuilder();
try {
while ((line = buffreader.readLine()) != null) {
text.append(line);
text.append('\n');
}
} catch (IOException e) {
return null;
}
return text.toString();
}
But the returned string is:
It's T st
How can I solve this? Thanks to
0
source to share
4 answers
Hi, I would try something like this:
StringBuilder str = new StringBuilder();
File file = new File("c:\\some_file.txt");
FileInputStream is = new FileInputStream(file);
Reader reader = new InputStreamReader(is, "UTF-8");
while(true){
int ch = reader.read();
if(ch < 0){
break;
}
str.append((char)ch);
}
String myString = str.toString();
If you want to write just use InputStreamWriter
with FileOutputStream
and set the correct encoding ... it works like a charm ...
Hope I can help :-)
+1
source to share