2012-04-23 31 views
2
private void copyMB() { 
    AssetManager assetManager = this.getResources().getAssets(); 
    String[] files = null; 
    try { 
     files = assetManager.list(assetDir); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    for(int i=0; i<files.length; i++) { 
     InputStream in = null; 
     FileOutputStream fos; 
     try { 
      in = assetManager.open(assetDir+"/" + files[i]); 

      fos = openFileOutput(files[i], Context.MODE_PRIVATE); 
      copyFile(in, fos); 
      in.close(); 
      in = null; 
      fos.flush(); 
      fos.close(); 
      fos = null; 
     } catch(Exception e) { 
      e.printStackTrace(); 
     }  
    } 
} 
private void copyFile(InputStream in, OutputStream out) throws IOException { 

    byte[] buffer = new byte[1024]; 
    int read; 

    while((read = in.read(buffer)) != -1){ 
     out.write(buffer, 0, read); 
    } 
} 

我的问题是UTF-8字符,如AAO是奇怪的看着字符替换。我如何确保我的InputStream阅读器使用UTF-8?在普通的Java中,它很容易编写... new InputStreamReader(filePath,“UTF-8”);但因为我是从资产的文件夹得到它我canot做到这一点(我不得不使用它不会采取“UTF-8”作为参数assetManager.open()方法。我如何通过assetManager读取.TXT资产为UTF-8的Android?

任何想法?:)

谢谢你的帮助。

回答

3

正如你自己写的:

new InputStreamReader(in, "UTF-8"); 

创建使用UTF-8编码的新的流阅读器。只要把它在copyFile()方法与你的InputStream作为参数。

+1

完美。这就是它; P – matphi 2012-04-23 11:56:38