Calling delete() on a File object, are the effects immediately visible? - android

If I call File.delete() are the effects on the underlying file system immediately visible? Can I write to the same file name in the same process/thread after without worrying about bad things happening? If not, is there a way to sync the underlying file system with just a File object?

File.delete() return a boolean telling you if the file has been correctly deleted.
So you could write something like :
if(yourFile.delete()) {
//keep doing what you want. You are now sure file has been deleted !
}
Also, before writing a new file, you could check if a file with the same name already exists.
From Oracle documentation :
Returns:
true if and only if the file or directory is successfully deleted; false otherwise
Oracle source
Also there is a SO thread that might help you

Related

Getting directory exists even deleted from gallery

I'm surprised when delete folder from gallery and getting that folder by programatically it's returning isExists() = true.
if(File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), directory).exists()) {
return true
}
Note: However it's happening mostly customised devices of android, is there any way to find directory is exists or not?
When you call File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), directory) you are actually creating folder and then you are checking for exist of that file, so you were always get true. Lookout documentation for constructor of File(File parent, String child).
Creates a new instance from a parent abstract
better to use concet string and get file like File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath()+ directory) to check exist or not.

How to check if a file is database in android programmatically?

How can I check If a file that I have selected from the list of files is database type or not in android ? The others files may be a text file, apk file or can be of any format. I assume not depending upon the extension of the file like .txt, .apk, .pdf etc as extension of the file can be changed also.
Thank You..!!
If you are using a subclass of SQLiteOpenHelper, you can create a new object with the file in question, and then call getReadableDatabase ().
If tha file is a database, no exception will be thrown.
Otherwise, a SQLiteDatabaseCorruptException will be trhown.
So you can surround your code with a try-catch block, and if an exception is thrown, then that file is not a database; otherwise, it is.
check by extension of file. Normally database file has extension as .db , .sql etc

what does it mean to delete a file on android

Naturally I thought to delete a file means to remove it from existence. So when I do
File file = new File(absPath);
....//add content
file.delete();
I expect that no further operation can be executed on file or it would throw an exception. But how come I can still add content to the file such as shown here Android saving Bitmap to SD card. So how do I delete a file so that it is completely gone? So that when someone go look through file manager, the file is no longer there? I am not in a position to test this now, so I was hoping for authoritative reference.
how come I can still add content to the file such as shown here Android saving Bitmap to SD card.
That code creates a new file after deleting the old one.
So how do I delete a file so that it is completely gone? So that when someone go look through file manager, the file is no longer there?
Call delete() on a File object that points to the file. Then, do not use that same File object to write to the file again, thereby creating a new file, as the code that you link to does.

Variable not declared until set in preferences

I'm working on an app that works with database and offers option to create backup on external storage. App gets the backup directory from preferences, but since it's not set when I run app for the first time, I use a default path instead, simply by getting path to external storage and creating a directory there.
For all this, I use global string value which keeps the backup path. But here's my problem - when I run my app for the first time, I declare the default path in onCreate() method. When I try to write this path to log, it works. But then I have onClick() method, where I listen to click on button, which calls dialog with export/import options. When I try to write the backup path to log from here, the string is empty.
Variable itself shouldn't be the issue, because here's the interesting thing - when I go to preferences and set the backup path from there (which is then saved to string variable just the same way as it was before), I can suddenly get the variable from the onClick method and everything works fine.
Just for the record, I tried setting different path and I've always been choosing same path in preferences as I used manually in the onCreate method.
Any ideas what could be the issue? I can write the whole condition for backup path used in onCreate also into the onClick method and that would probably solve this issue, but I would like to avoid duplicite code and it's also really bugging me.
EDIT:
Here's code of the condition I use in the onCreate method.
location = PreferenceManager.getDefaultSharedPreferences(this);
if(location.getString("backup_location", "").equals("")) {
String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state)) {
File file = new File(Environment.getExternalStorageDirectory().toString() + "/simpledbmanager_backup");
if(!file.exists()) {
file.mkdirs();
}
this.backup = file.getAbsolutePath().toString();
}
}
else {
this.backup = location.getString("backup_location", "");
}

Android file atomic operation

I need to read some data from file in internal storage, then remove and rewrite file with new data. What is the best way to do it(safest method for data)?
I'd first rename the original file (append something like .orig), then read it, write the new file and when all that's successful, remove the renamed original file. This ensures the most that no data gets lost.

Categories

Resources