我想检查文件夹中是否存在文件,但我不想创建一个新文件。Android;检查文件是否存在,但不创建新文件
File file = new File(filePath);
if(file.exists())
return true;
此代码检查没有创建新文件吗?
我想检查文件夹中是否存在文件,但我不想创建一个新文件。Android;检查文件是否存在,但不创建新文件
File file = new File(filePath);
if(file.exists())
return true;
此代码检查没有创建新文件吗?
你的代码块不会创建一个新的,它只会检查它是否已经存在,没有其他。
File file = new File(filePath);
if(file.exists())
//Do something
else
// Do something else.
当您使用此代码,你是不是创建一个新的文件,它只是创建该文件的对象引用,并测试它是否存在与否。
File file = new File(filePath);
if(file.exists())
//do something
当你说“在你的包文件夹中”,你的意思是你的本地应用程序文件?如果是这样,你可以使用Context.fileList()方法得到它们的列表。只需遍历并查找您的文件。假设您保存了原始文件Context.openFileOutput()。
示例代码(在一个Activity):
public void onCreate(...) {
super.onCreate(...);
String[] files = fileList();
for (String file : files) {
if (file.equals(myFileName)) {
//file exits
}
}
}
在Path类的methods
是句法,这意味着它们在路径实例进行操作。但最终,你必须访问file
系统验证特定路径存在
File file = new File("FileName");
if(file.exists()){
System.out.println("file is already there");
}else{
System.out.println("Not find file ");
}
它的工作对我来说:
File file = new File(getApplicationContext().getFilesDir(),"whatever.txt");
if(file.exists()){
//Do something
}
else{
//Nothing
}
public boolean FileExists(String fname) {
File file = getBaseContext().getFileStreamPath(fname);
return file.exists();
}
这似乎是正确的。 – 2013-04-26 13:41:21
可能重复的[测试如果文件存在](http://stackoverflow.com/questions/2786655/test-if-file-exists) – piokuc 2013-04-26 13:42:20
@Kunok我在查看你的编辑评论:*删除的话,如**坦克**,因为他们是...... *:P – 2016-01-01 10:28:13