问题
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
回答1:
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.
回答2:
First of all, you need to determine the encoding of the file /res/raw
If on UNIX, you may type following commands
file /res/raw
And then specify the proper encoding in
inputreader = new InputStreamReader(inputStream, "UTF-8");
回答3:
I had a file that also gave me an output similar to 'This is a T�s�t', and for me, setting the charsetName to UTF-16 did the trick
回答4:
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 setup right Encoding... it works like charm...
I hope i could help :-)
来源:https://stackoverflow.com/questions/14335166/read-raw-file-with-unicode-characters