【问题标题】:How to find if the file exist or not in Android? [duplicate]如何在Android中查找文件是否存在? [复制]
【发布时间】:2014-06-15 04:42:39
【问题描述】:

我已经写了下面的代码来写一个文件-

FileOutputStream fos = context.openFileOutput("MyFile",Context.MODE_PRIVATE);

ObjectOutputStream os = new ObjectOutputStream(fos);

//Saving my list
os.writeObject(EmailStackClass.list);
os.close();

如何识别文件是否已经存在?因为如果文件是以前创建的,那么我必须将我的数据附加到前一个文件中。 请帮帮我...

【问题讨论】:

标签: android file storage fileoutputstream objectoutputstream


【解决方案1】:

试试下面的代码:-

File f = new File("MyFile"); // PATH OF YOUR FILE
        if(f.exists())
        {
            System.out.println("file exist");
        }
        else
        {
            System.out.println("file not exist");
        }

【讨论】:

  • 我总是得到文件不存在
  • @user2166895 这意味着您的文件不存在!!!
【解决方案2】:

首先检查这个..

File file = new File("dirPath", "fileName");
if(file.exists()){
        // file exists
}else{
        // file not exists
}

希望对你有所帮助..

【讨论】:

  • 我不知道 dirPath ,
  • 您是如何创建该文件的?
  • FileOutputStream fos = ctxt.openFileOutput("MyFile", Context.MODE_PRIVATE); ObjectOutputStream os = new ObjectOutputStream(fos); os.writeObject(EmailStackClass.list); os.close();
  • 从哪里“MyFile”这个文件可能在 SDCARD/PhoneStorage/ASSETS ??
【解决方案3】:

您可以检查如下...

File file = getBaseContext().getFileStreamPath(fileName);
if(file.exists()) {

    //do something with the existed file
}

【讨论】:

    猜你喜欢
    • 2018-09-27
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    • 2020-03-20
    • 2014-03-06
    • 2022-01-12
    • 1970-01-01
    • 2015-10-18
    相关资源
    最近更新 更多