Issue
I have a file in the /res/raw (R.raw.test) folder with the following content:
This is a Tésêt
I want to read it into a string. My current code is:
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 string returned is:
This is a T�s�t
How can I solve this? Thanks
Solution
Your code seems OK. String returned is OK also except that you're trying to view it in viewer not supporting UTF-8
. I've run your code from groovyConsole
which is UNICODE aware and it displays UTF-8
string perfectly.
Answered By - Archer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.