3

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?

dreftymac
  • 31,404
  • 26
  • 119
  • 182
androniennn
  • 3,117
  • 11
  • 50
  • 107
  • This may help you http://stackoverflow.com/questions/494869/file-changed-listener-in-java – Nemo Sep 12 '11 at 11:28

5 Answers5

4

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());
}
Kurru
  • 14,180
  • 18
  • 64
  • 84
  • And when i want to make some treatement when `lastModified` variable has been modified, how can i code that ? – androniennn Sep 12 '11 at 11:29
  • 1
    if (storedTime < file.lastModified()) { // file has changed } – Kurru Sep 12 '11 at 11:34
  • The operator < is undefined for the argument type(s) Date, long . I think we can not compare two dates – androniennn Sep 12 '11 at 11:49
  • 1
    Thats why I didn't use the date object, but instead "file.lastModified()" which returns a long value. Which supports <. The Data class is just used here if you wanted to check the values etc – Kurru Sep 12 '11 at 12:37
4

Take a look at FileObserver class documentation.

Ron
  • 24,175
  • 8
  • 56
  • 97
2

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

Community
  • 1
  • 1
xDragonZ
  • 12,502
  • 6
  • 37
  • 52
1

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

Rich
  • 36,270
  • 31
  • 115
  • 154
  • and how can i store the previous modification date and control if the date has been changed ? – androniennn Sep 12 '11 at 11:25
  • @androniennn -- lots of ways... such as use SharedPreferences. I.e., the same way you would store any other value you wanted a persistent reference to. – mah Sep 12 '11 at 11:27
  • @andronienn: If you're going to be dealing with a large number of files, an SQLite database would be best. With the index set on the filename, lookups are very efficient and fast – Kurru Sep 12 '11 at 11:29
  • @Rich: Don't need a sharedpreference to store a previous file modification date, i think it's useless. But can i store it in a String then check the previous string(date) with the new String(date) – androniennn Sep 12 '11 at 11:32
  • 1
    You have a number of options, but it all depends on how you want to persist the data. SharedPreferences doesn't require a lot of code, so I don't know why you'd call it useless. If you need to persist this information between user sessions (meaning, if the user uses your app, modifies the file, and then uses your app a week later...do you need to know then that it was modified), then you need to persist this information. SharedPreferences, SQLite database, save info to file, whatever...you have options, but if you want the information you have to write some code – Rich Sep 12 '11 at 11:39
0

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.

John Mark
  • 2,458
  • 2
  • 21
  • 17