读取文件作为字符串 [英] Read file As String

查看:160
本文介绍了读取文件作为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要加载XML文件作为字符串中的Andr​​oid,所以我可以加载到TBXML XML解析器库并解析它。实施我现在要读取该文件作为字符串大约需要2秒甚至一些KB的一个非常小的xml文件。是否有可以读取一个文件作为字符串中的Java / Android的?

任何已知的快速方法

这是在code我现在有:

 公共静态字符串readFileAsString(字符串文件路径){

    字符串结果=;
    档案文件=新的文件(文件路径);
    如果(file.exists()){
        // byte []的缓冲区=新的字节[(INT)新File(文件路径).length();
        的FileInputStream FIS = NULL;
        尝试 {
            // F =新的BufferedInputStream(新的FileInputStream(文件路径));
            //f.read(buffer);

            FIS =新的FileInputStream(文件);
            char的电流;
            而(fis.available()大于0){
                电流=(char)的fis.read();
                结果=结果+将String.valueOf(电流);

            }

        }赶上(例外五){
            Log.d(导游,e.toString());
        } 最后 {
            如果(FIS!= NULL)
                尝试 {
                    fis.close();
                }赶上(IOException异常忽略){
            }
        }
        //结果=新的String(缓冲区);
    }
    返回结果;
}
 

解决方案

在code最后采用的是从以下方面:

  http://www.java2s.com/$c$c/Java/File-Input-Output/ConvertInputStreamtoString.htm

公共静态字符串convertStreamToString(InputStream的是)抛出异常{
    的BufferedReader读卡器=新的BufferedReader(新InputStreamReader的(是));
    StringBuilder的SB =新的StringBuilder();
    串线= NULL;
    而((行= reader.readLine())!= NULL){
      sb.append(线).append(\ N);
    }
    reader.close();
    返回sb.toString();
}

公共静态字符串getStringFromFile(字符串文件路径)抛出异常{
    文件F1 =新的文件(文件路径);
    的FileInputStream鳍=新的FileInputStream(FL);
    字符串RET = convertStreamToString(翅);
    //确保关闭所有流。
    fin.close();
    返回RET;
}
 

I need to load an xml file as String in android so I can load it to TBXML xml parser library and parse it. The implementation I have now to read the file as String takes around 2seconds even for a very small xml file of some KBs. Is there any known fast method that can read a file as string in Java/Android?


This is the code I have now:

    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();
                } catch (IOException ignored) {
            }
        }
        //result = new String(buffer);
    }
    return result;
}

解决方案

The code finally used is the following from:

http://www.java2s.com/Code/Java/File-Input-Output/ConvertInputStreamtoString.htm

public static String convertStreamToString(InputStream is) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
      sb.append(line).append("\n");
    }
    reader.close();
    return sb.toString();
}

public static String getStringFromFile (String filePath) throws Exception {
    File fl = new File(filePath);
    FileInputStream fin = new FileInputStream(fl);
    String ret = convertStreamToString(fin);
    //Make sure you close all streams.
    fin.close();        
    return ret;
}

这篇关于读取文件作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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