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.
Related
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();
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.
I have a program, written in Java, which originally used its directory in Program Files to write files accessible to all users of this program. This required our users to run as administrator all the time. In an effort to alleviate that, we decided to move files which needed to be written during regular usage to the ProgramData folder using the %ALLUSERSPROFILE% environment variable. Using a subfolder in this directory for our application works great if it is designated as writable during the installation process, which works fine using NSIS.
The problem comes with upgrading existing users. The Java File API provides setWritable but this does not appear to work after testing on development machines. It looks as though the new file API with Java 7 would solve this problem, but with no release date on the horizon I would rather not wait.
It seems the simplest solution would be to use JNA to call the appropriate Windows API call to set this directory writable. Since upgrading the software necessitates admin rights, similar to installing, it should let this change go through fine. However, I'm unsure where to start, having never used JNA before or the Windows API. Suggestions as to which Windows library to load and what functions to call would be appreciated, especially if someone has encountered a similar problem before.
Well, I'm glad you gave some background...You could use JNA, but the easier way would be to execute a call to the command-line utility cacls. It's included by default in Windows XP installations, I believe, so it should do the trick for you. Try Runtime.getRuntime().exec("C:\\Windows\\System32\\cacls.exe"+options)
Check out the documentation here -> http://technet.microsoft.com/en-us/library/bb490872.aspx
I use the follow line:
Runtime.getRuntime().exec( "C:\\Windows\\System32\\icacls.exe \"%ProgramData%\my application" /grant *S-1-5-32-545:(OI)(CI)(W,M)" );
S-1-5-32-545 is the SID for BUILTIN\Users because the name work only on English systems. https://support.microsoft.com/de-de/kb/163846
This give the BUILTIN\Users write access to all files in the given directory independent which user has create it.
I want to create a program using Java for Automatically copied USB's data when it's insert to machine. How I do it?
There is no such thing as "USBs data", the very concept doesn't exist.
There is nothing specific in Java SE for do this job.
I may think of two ways to get that working:
Write a Java program that starts on boot (maybe a service), the prog scans continously available "drives" (D:,E:,F: ... in Windows, mount on Linux), the USB flash may be marked with a specific folder/file name (eg. COPY_USB_). That can be done with the File class.
Write a Java program that get invoked on plug-in. I know that can be done on Linux with hotplug-script support.
I want to get the name of all active process on a Win2000 machine. I know that "tlist -s" is the command, but to use this, the "tlist.exe" has to be extracted from the Win2000 CD-ROM. I also know "tasklist" only works on XP or greater.
I am running my tests on VM Ware, and do not have a CD-ROM. Is there another way to programmatically get the task list on Win2000?
I ended up just using JNI to handle this case across all platforms that I needed. Refer to another post of mine (which was trying to use SWT to obtain this list, but it gives a good JNI downloadable example that is useful):
Get task manager list via SWT?
In Java without calling a 3rd party application (like say PsList)? Not without JNI.