new File(path) always actually creates a file on android?

喜欢而已 提交于 2019-12-05 19:21:17
JanBo

For some strange reason it turned out that new File created a directory every time...

instead of checking if (!f.exists()), I changed it to checking if (!f.isFile())

In that case i create a new file and it works good, the next time i run it the file is already on the sd card...

The way that worked was nearly like yours:

File f = new File(Environment.getExternalStorageDirectory(), "a directory");
if(!f.exists){
// do something
}

and to check whether a file exists or not is almost the same way:

File f = new File(Environment.getExternalStorageDirectory() + "/a directory/" + "a file");
if(!f.exists){
// do something
}

I hope it can help you out, because it didn't create a file or directory in my app. It just checked the path.

this may helps you, try like

if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                    //handle case of no SDCARD present
                } else {
                    File file = new File(Environment.getExternalStorageDirectory()
                         +File.separator
                         +"myDirectory" //folder name
                         +File.separator
                         +"myFile.example"); //file name

                    if(file.exists()){
                        Toast.makeText(MainActivity.this, "Not Create ", 12).show();
                    }else{
                        file.mkdirs();
                        Toast.makeText(MainActivity.this, "Create ", 12).show();
                    }

                }

Try this

    File[] files = filedir.listFiles();
    for (File file2 : files) {
        if (file2.isDirectory()) {
            Toast.makeText(this, "directory", Toast.LENGTH_LONG).show();
        } else {  
            if (file2.getName().equals(DATABASE_NAME)) {
                Toast.makeText(this, "File found",Toast.LENGTH_LONG).show();
            }
          else{Toast.makeText(this, "File not found",Toast.LENGTH_LONG).show();
        }
}
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!