繁体   English   中英

如何正确关闭文件到字符串输入流? (IOUtils FileUtils)

[英]How to properly close file-to-string input stream? (IOUtils FileUtils)

我的应用程序中有两个文件到字符串进程(一个实际处理资产文件)。
如果我在同一个文件上重复这些过程中的任何一个,我会得到OutOfMemoryErrors。
我怀疑这可能是因为我没有正确关闭流,因此可能导致创建多个流,这可能导致我的应用程序内存不足。
这是两个过程的代码:

我的资产文件到字符串进程。
正如你所看到的,我有一些东西可以关闭流,但我不知道它的格式是否正确。

try 
{
      myVeryLargeString = IOUtils.toString(getAssets().open(myAssetsFilePath), "UTF-8");
      IOUtils.closeQuietly(getAssets().open(myAssetsFilePath));
} 
catch (IOException e) 
{
      e.printStackTrace();
}
catch(OutOfMemoryError e)
{
      Log.e(TAG, "Ran out of memory 01");
}



我的文件到字符串进程。
我不知道如何关闭这个流(如果有一个流甚至根本关闭)。

myFile01 = new File(myFilePath);
try 
{
      myVeryLargeString = FileUtils.readFileToString(myFile01, "UTF-8");
} 
catch (IOException e) 
{
      e.printStackTrace();
}
catch(OutOfMemoryError e)
{
      Log.e(TAG, "Ran out of memory 02");
}

很难说什么可能导致OOME,但关闭应该是这样的

InputStream is = getAssets().open(myAssetsFilePath);
try {
    myVeryLargeString = IOUtils.toString(is, "UTF-8");
} finally {
    IOUtils.closeQuietly(is);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM