Keeping track of a file in java 1.6 - java

I want to keep track of multiple files in a location in a unix box, and delete them if no one is using them for a long time.
I am trying to search for reference counting but did not get much help on google.
I also saw FileTime in java 7 which can give me the last accessed time , but i have to the above work using java 6.
If anyone has any ideas or good reference to reference counting and how i can use it, it will be great.

You can use apache common IO. Set observer on your dir which checks the dir and notifying listeners of create, change or delete eventslisten. By this it is possible to track on which dir has been working by user and others are not.

Okay, so we want last access time.
You can have a read of Get the Last Access Time for a File for some further info.
You could try and have a look at http://jdevel.wordpress.com/2011/04/08/file-last-access-time-in-java-on-linux/ for a possible soultion, but this is Linux, not Unix.
Finally, you could borrow the same idea and use JNA or JNI

There is no module for File watching till JDK 1.6. What you can do is, you can write your own file watcher by seeing the
lastModified() method of the java.io.File class.
It returns you the last modified time as a long value and you keep a watch on this file during a regular interval. If the total time difference
(presentTime - lastModifiedTime)
exceeds you time criteria, you can delete the file.

Related

Get information about changed file in Java

I'm trying to monitor files in java. When a file modification will happen I want to know:
Which process did the change.
What has changed.
Also, I know that there is a way to change the "last modified date" in a file, so I want to check if someone has changed that field.
I tried "commons.io", "DefaultFileMonitor" and "WatchService", but all the information I could get from them was if a change has occurred and the file that was changed.
Unless you're on a weird OS I know absolutely nothing about, you would need to use some mechanism you implement to track who is changing your file. The OS doesn't keep track of that. It also doesn't track what has changed.
So I don't believe you can do #1 unless you can get every process that MIGHT change the file to track that it did a change.
You could do #2 if you kept copies of the file then do a comparison.
I am pretty sure you cannot do that. Neither OS nor Java is storing such information. Maybe using some kernel calls.

file.lastModified() function does not give the updated value

file.lastModified() not updated , although I change the file content , I work on Windows.
I use File of java , what could be the problem ? Do I need to do some kind of refresh from the code ?
Java, at least 1.8 appears to be caching file attributes. I created a C++ application that uses the Win32's FindFirstFile to get the file information and verify that the last modified value is updated every time. And it does. But Java's File and Files both seem to not always get the latest value. Even tried a one second delay between writes and it didn't always get updated. I'm using Files.write to modify the file so I would hope it's closing it. Not sure if there's a good solution for this in Java, which is unfortunate.
UPDATE:
I found that Files.getLastModifiedTime is a solution for this. But it's only available for Java 1.8 or later.
This behaviour is platform-dependent. Windows doesn't update the metadata of a file being modified until it is closed.
If the file was loaded from the classpath resources, files.lastModified() might not detect the change after an edit is made.
However, when the file was loaded from the outside of the application, it did work well in my case (Windows 10, Java 10).
I've tested it by running files.lastModified() on a scheduled thread, the file reference was loaded once at the JVM startup.
So depending on how your file is loaded, it might affect how file modification is read.
If you are setting lastModified time. it should work fine
File file =new File("fileName");
// other operations
file.setLastModified(new Date().getTime()); // set time
Now you can get lastUpdated time back.
file.lastModified();

Undo file/folder operation using java

I am looking to implement undo/redo file/folder functionality for edit/delete/move etc using java. Is that possible ?
Detail: I am watching a given folder using javaFileWatcher, i get a message from watcher that if any file/folder created/edited/deleted/renamed. now i want if any user has deleted/created any file/folder i would be able to undo it using java. can any one help.
Note: I am using windows
Thanks in advance.
Not possible, a delete is final, there is no recycle bin like in the OS, you have to implement it by yourself.
As a kickoff idea have a look at the Command pattern. This is usually how undo/redo things are implemented. The idea is that you kind of store everything that is "important" about an operation into a single object and you store those in an ordered collection.
In your case the fact that you want to monitor the FS and not your own data model makes things really hard. However if you can afford redundant data storage then hypothetically it can be done I think. (I suggest you reading about the way git SCM works. I mean that it stores everything in a single .git folder and it is capable of restoring any previous state in its so called working directory from the compressed data it stores.)
Actually if you could involve git into your program then it would be much easier. I am thinking that you have a git repo initialized in the directory that you want to supervise and then every time that you are being notified about some FS change, you can ask git (git status) to tell you which files got changed actually. Even deleted file can be restored then using simple git commands.
This is far from a complete answer to your question however I might have given some ideas. Good luck.

Getting Java Filesystem-Folder onchange event (Like Dropbox)

I want to sync a folder like Dropbox. If in my Folder is changed a file or a folder, I want to get an Event, which starts my synchronisation Class. How can I get such an Event without scanning this folder by an Intervall?
You have at least two options. You can either reinvent the wheel as Arpit suggested or you can also use the WatchService API.
You can find a WatchService tutorial here.
Some start for you:
get the list of all files : yourdir.listFiles()
now for each file in filelist:
file.getLastModified()
if it is equal to current time or differ from lastSynctime(you need to maintain it in your sync class) then sync it.

Java set accessed date on file

Is there a way in java to set the access time without setting the date modified time as well?
This will be in essence a touch -a command.
the setLastModified method in File updates both the access time as well as the date modified.
We are currently using java 6. Moving to 7 wouldn't be out of the question.
You can use Files.setAttribute() from Java 7:
FileTime fileTime = FileTime.fromMillis(millis);
Files.setAttribute(path, "lastAccessTime", fileTime);
The string "lastAccessTime" can be found in the description of the BasicFileAttributeView, which also provides an alternative way to set this property (together with Files.getFileAttributeView()):
Files.getFileAttributeView(path, BasicFileAttributeView.class).setTimes(null, fileTime, null);
I'm not aware of any pure Java way that works in Java 6 or earlier.
I guess opening an input stream on the file should modify its access time. But I don't know of any API to directly modify this attribute in Java 6.
There is a private sun API to check access time on a file(How to prevent ShellFolder.getFolderColumns(...) from crashing a lot), but no way to set it from what I've found. Though access time should be updated everytime you open the file, so maybe try opening it in Java.
You could check if the old JDIC project has the option: http://javadesktop.org/articles/jdic/index.html
Or this library: http://www.teamdev.com/jxfilewatcher/
Otherwise you might have to look up the console command for the OS's you want to support and call from Java.

Categories

Resources