![](/img/trans.png)
[英]Do I need to close the input stream manually after using IOUtils.toString(input) of commons-io?
[英]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.