在没有循环的android中读取完整的文件

时间:2016-10-26 13:28:19

标签: java android cordova

我需要一个解决方案来读取存储在内部存储中的文本文件。 我不想逐行阅读。不使用循环如何读取完整的文本文件并将其存储到字符串中。

BufferedReader br;
String line;
String data = "";
// String text="";
try {
    br = new BufferedReader(new FileReader(new File(Environment.getExternalStorageDirectory(), "queue_mgr.txt")));
    while ((line = br.readLine()) != null) {
        sb.append(line);
    }
    br.close();
}

1 个答案:

答案 0 :(得分:0)

您可以使用大字节缓冲区并获得一些效率:

try
{
  InputStream in = new FileInputStream (from);
  OutputStream out = new FileOutputStream (to);

  // Transfer bytes from in to out
  byte[] buf = new byte[1024 * 10]; // 5MB would be about 500 iterations
  int len;
  while ((len = in.read (buf)) > 0)
    out.write (buf, 0, len);

  in.close ();
  out.close ();
  }
}
catch (FileNotFoundException e)
{
  ...
}
catch (IOException e)
{
  ...
}