【问题标题】:I can not read entire text file on android我无法在 android 上读取整个文本文件
【发布时间】:2013-06-02 13:32:23
【问题描述】:

请您帮帮我,我无法阅读整个文本文件。

我只能从文本文件中读取大约 100 行。

文字大小 = 61535

代码如下。

String path = Environment.getExternalStorageDirectory().getAbsolutePath();
path = path + "/testFile.txt";
StringBuilder fileData = new StringBuilder();
BufferedReader reader = new BufferedReader(new FileReader(path));
char[] buf = new char[1024];
int numRead=0;

while((numRead=reader.read(buf)) != -1)
{
    String readData = String.valueOf(buf, 0, numRead);
    fileData.append(readData);
}

reader.close();

期待您的回复。

【问题讨论】:

  • 任何异常(查看 Logcat)?
  • 100 行之后会发生什么????
  • @user2445150 你能发布xml文件吗???
  • @Michael Butscher 你好,没有例外。
  • @Mathew Foscarini 什么也没发生。 String 中没有更多字符。

标签: android file bufferedreader


【解决方案1】:
    File yourFile = new File("Path");
    InputStream input= new BufferedInputStream(new FileInputStream(yourFile), 8086); 
    String str = convertStreamToString(input);

InputStrem转String的方法

 private String convertStreamToString(InputStream is) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();

            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return sb.toString();
        }

【讨论】:

  • 感谢您的快速回复。但我得到了同样的结果。我只得到了大约 100 行。请告诉我解决此问题的另一种方法吗?
  • 我认为这个答案或原始代码没有任何问题。您拥有的文件只有 100 行,或者它没有按照您的想法执行。
  • 是的,我发现该代码没有任何问题。日志有问题..
【解决方案2】:

如果您没有在 xml 文件中使用 Scrollview,那么这可能是您面临的问题!

你用过Scrollview吗?

请发布您活动的相应 Xml 文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-19
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2015-10-01
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多