开发者

Problems with Swedish letters using ByteArrayOutputStream in Android

开发者 https://www.devze.com 2023-04-03 19:43 出处:网络
I am using ByteArrayOutputStream to put text in a Text View from an IputStream. This works fine but...

I am using ByteArrayOutputStream to put text in a Text View from an IputStream. This works fine but... I am from Sweden and when I put a text with some special Swedish letters it puts ? instead of the actual letter. The system have no problems with this letters otherwise. Hope someone out there can give me a hint about what to do.

Perhaps I shall show the code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView helloTxt = (TextView)findViewById(R.id.hellotxt);
    helloTxt.setText(readTxt());
}

 private String readTxt(){
 InputStream inputStream = getResources().openRawResource(R.raw.hello);
 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
 int i;
 try {
 i = inputStream.read();
 while (i != -1)
  {
   byteArrayOutputStream.write(i);
   i = inputStream.read();
  }
  inputStream.close();
} catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
}

 return byteArrayOutputStream.toString();
}
}

I also tied this, get it from the forum (Selzier): Nice peace but still no Swedish letters in the output:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView tv = (TextView)findViewById(R.id.txtRawResource);  
    tv.setText(readFile(this, R.raw.saga));
}

private static CharSequence readFile(Activity activity, int 开发者_运维百科id) {
    BufferedReader in = null;
    try {
        in = new BufferedReader(new InputStreamReader(
                activity.getResources().openRawResource(id)));
        String line;
        StringBuilder buffer = new StringBuilder();
        while ((line = in.readLine()) != null) buffer.append(line).append('\n');
        return buffer;
        } 
    catch (IOException e) {
        return "";
    } 
    finally {
        closeStream(in);
    }
}

/**
 * Closes the specified stream.
 */
private static void closeStream(Closeable stream) {
    if (stream != null) {
        try {
            stream.close();
        } catch (IOException e) {
            // Ignore
        }
    }
}
}


You are using the wrong encoding when you read/write the stream. Use UTF-8.

 outputStream.toString("UTF8")

Edit: try this approach posted here. I think it can also be a issue if your file has a BOM. Use NotePad++ or another editor to remove it.

 public static String readRawTextFile(Context ctx, int resId)
 {
     InputStream inputStream = ctx.getResources().openRawResource(resId);

     InputStreamReader inputreader = new InputStreamReader(inputStream);
     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();
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号