我如何通过assetManager读取.TXT资产为UTF-8的Andr​​oid? [英] How do I read .txt assets as UTF-8 in Android through the assetManager?

查看:220
本文介绍了我如何通过assetManager读取.TXT资产为UTF-8的Andr​​oid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 私人无效copyMB(){
    AssetManager assetManager = this.getResources()getAssets()。
    的String []文件= NULL;
    尝试{
        文件= assetManager.list(assetDir);
    }赶上(例外五){
        e.printStackTrace();
    }
    的for(int i = 0; I< files.length;我++){
        在的InputStream = NULL;
        FileOutputStream中FOS;
        尝试{
            在= assetManager.open(assetDir +/+文件[I]);            FOS = openFileOutput(文件[I],Context.MODE_PRIVATE);
            copyFile(中,FOS);
            附寄();
            在= NULL;
            fos.flush();
            fos.close();
            FOS = NULL;
        }赶上(例外五){
            e.printStackTrace();
        }
    }
}
私人无效copyFile(在的InputStream,OutputStream的了)抛出IOException    字节[]缓冲区=新的字节[1024];
    INT读;    而((读= in.read(缓冲))!= -1){
        out.write(缓冲,0,读);
    }
}

我的问题是UTF-8字符,如AAO被奇怪的看着字符替换。我怎样才能确保它的InputStream我的读者使用UTF-8?在正常的Java它会很容易写作......新的InputStreamReader(文件路径,UTF-8);但自从我从资产文件夹中得到它我canot做到这一点(我不得不使用它不会采取UTF-8作为参数assetManager.open()方法。

任何想法? :)

感谢您的帮助。


解决方案

你自己写的:

 新的InputStreamReader(在UTF-8);

创建了UTF-8编码一个新的流阅读器。只需把它放在 copyFile()方法与你的的InputStream 作为参数。<​​/ P>

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);
    }
}

My problem is that UTF-8 character such as åäö is replaced by wierd looking characters. How can I make sure that it my InputStream reader uses UTF-8? In normal Java it would be easy Writing … new InputStreamReader(filePath, "UTF-8"); But since I am getting it from the Asset folder I canot do that (I have to use the assetManager.open() method which wont take "UTF-8" as an argument.

Any ideas? :)

Thank you for your help.

解决方案

As you write yourself:

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

creates a new stream reader with Utf-8 encoding. Just put it in the copyFile() method with your InputStream as the argument.

这篇关于我如何通过assetManager读取.TXT资产为UTF-8的Andr​​oid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆