如果文件存在测试 [英] Test if file exists
本文介绍了如果文件存在测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在Android开放的文件是这样的:
I'm trying to open a file in android like this :
try
{
FileInputStream fIn = context.openFileInput(FILE);
DataInputStream in = new DataInputStream(fIn);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
if(in!=null)
in.close();
}
catch(Exception e)
{ }
,但如果该文件不存在找不到文件异常。我想知道我怎么能测试,如果该文件试图打开它之前存在。
, but in case the file does not exists a file not found exception is thrown . I'd like to know how could I test if the file exists before attempting to open it.
推荐答案
我觉得最好的办法知道文件是否存在,实际上并没有试图打开它,如下:
I think the best way to know if a file exists, without actually trying to open it, is as follows:
File file = getContext().getFileStreamPath(FILE_NAME);
if(file.exists()) ...
希望帮助,再见!
Hope that helps, bye!
这篇关于如果文件存在测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文