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
Related
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
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.
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.
Using Eclipse, Android SDK.
I have a text file full of data that I need pulled in. (For now, it's the easier the way, eventually I'm going to need to scrape dynamic data from a URL, but for now I have the test data I need in this text file).
I've created a class to open this file, but no matter how I try to open it I keep geeting "file not found" exceptions.
I've tried putting my "data.txt" file in various relative paths (within my App):
- "/AppName/"
- "/AppName/src/com/example/appname/data.txt"
I've tried passing different relative paths. I've tried putting the text file in the same path of the .java class file that's trying to open it, and it still can't find it! What am I doing wrong?
What am I doing wrong?
You have two main options of where to store this file within your project directory: assets/ and res/raw/.
If you use assets/, you can call getAssets() on your Activity (or other Context), and on there call open() with the relative path within assets/ to get an InputStream on this file (e.g., assets/data.txt would be accessed via getAssets().open("data.txt")).
If you use res/raw/, you can call getResources() on your Activity (or other Context), and on there call openRawResource(), passing in the R.raw value based upon your filename (e.g., res/raw/data.txt would be accessed via getResources().openRawResource(R.raw.data)).
Use /res/raw directory. I read sound files from raw:
InputStream soundFile1 = context.getResources().openRawResource(soundOneId);
Where context is a base context of activity passed to the class.
http://developer.android.com/reference/android/content/Context.html
In the application there is a deleteFile(String path) method implemented which has to delete the file located at the given path.
If the path contains path separator (character /), the method throws an exception, but to delete a specific file a full path has to be used and it contains a separator sign. Due to conflicting conditions I haven't found a way to successfully delete the file.
Does anyone know how to bypass the problem?
Without knowing what the exception is it is hard to say. In general it is best to avoid hardcoding path separators and instead use the File.separator symbol.
How are you going about deleting the File? The File class has a delete method; is that what you are using?
I'm guessing the problem might actually be unrelated to the things I mentioned up top, and instead be due to permissions. Are you trying to access files or directories that your program has no right to access?
According to javadoc of Application.deleteFile path couldn't contain path separator which is : at Unix and ; at Windows, so at least according to java doc you can delete file with a full path.
Edit:
'\' is File.separtor not pathSeparator.