androniennn
androniennn

Reputation: 3137

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/reference/java/io/File.html.

Any ideas please?

Upvotes: 3

Views: 7874

Answers (5)

John Mark
John Mark

Reputation: 2528

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.

Upvotes: 0

Ron
Ron

Reputation: 24235

Take a look at FileObserver class documentation.

Upvotes: 4

Kurru
Kurru

Reputation: 14331

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());
}

Upvotes: 4

Rich
Rich

Reputation: 36806

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()

Upvotes: 1

xDragonZ
xDragonZ

Reputation: 12662

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

Upvotes: 2

Related Questions