2012-04-23 27 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