相关疑难解决方法(0)

读取文件为字符串

我需要在android中加载一个xml文件作为String,这样我就可以将它加载到TBXML xml解析器库并解析它.我现在将文件读取为String的实现大约需要2秒,即使对于某些KB的非常小的xml文件也是如此.有没有已知的快速方法可以在Java/Android中将文件读取为字符串?


这是我现在的代码:

public static String readFileAsString(String filePath) {
    String result = "";
    File file = new File(filePath);
    if ( file.exists() ) {
        //byte[] buffer = new byte[(int) new File(filePath).length()];
        FileInputStream fis = null;
        try {
            //f = new BufferedInputStream(new FileInputStream(filePath));
            //f.read(buffer);

            fis = new FileInputStream(file);
            char current;
            while (fis.available() > 0) {
                current = (char) fis.read();
                result = result + String.valueOf(current);
            }
        } catch (Exception e) {
            Log.d("TourGuide", e.toString());
        } finally {
            if (fis != null)
                try {
                    fis.close(); …
Run Code Online (Sandbox Code Playgroud)

string optimization android

51
推荐指数
5
解决办法
10万
查看次数

标签 统计

android ×1

optimization ×1

string ×1