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 文字が奇妙な文字に置き換えられることです。InputStream リーダーが UTF-8 を使用していることを確認するにはどうすればよいですか? 通常の Java では簡単に書けます… new InputStreamReader(filePath, "UTF-8"); しかし、Asset フォルダーから取得しているので、それを行うことはできません (「UTF-8」を引数としてとらない assetManager.open() メソッドを使用する必要があります。

何か案は?:)

ご協力ありがとうございました。

4

2 に答える 2

4

あなた自身が書いているように:

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

Utf-8 エンコーディングで新しいストリーム リーダーを作成します。あなたを引数としてcopyFile()メソッドに入れるだけです。InputStream

于 2012-04-23T11:20:09.840 に答える