How to detect file modification?
I have a stored file containing some data. I want to detect if it has been modified, and then address the issue if necessary.
I thought about controlling for the number of lines in the file, but I am stuck.
Moreover, there is no method to perform this: http://developer.android.com/r开发者_JAVA百科eference/java/io/File.html.
Any ideas please?
Using an MD5 checksum would be one, very intensive way to do this, however if you compare the timestamps for the "modified date" data in the file, you should be able to efficiently find the files that have been modified.
This should help you start
File file = new File(absolute_file_location);
if (file.exists())
{
Date lastModified = new Date(file.lastModified());
}
Take a look at FileObserver
class documentation.
You can try use md5 file check to check the checksum of the files.
Java md5 checksum : Getting a File's MD5 Checksum in Java
Or try this http://www.rgagnon.com/javadetails/java-0490.html
I think there are others method better than this
Keep track of the timestamp that the file was last modified using the lastModified method of File
http://developer.android.com/reference/java/io/File.html#lastModified()
Another approach is watching the file for changes using Java's WatchService
.
https://docs.oracle.com/javase/tutorial/essential/io/notification.html
I'm not sure how well it works in Android, though.
精彩评论