Get the Last Access Time for a File - java

I know that using File object we can get the last modified time for a File (i.e. File.lastModified()). But, my requirement is to get the last accessed time for a File in Java. How do I get it?

You will need to use the new file I/O API (NIO2) which comes with Java 7. It has a method lastAccessTime() for reading the last access time.
Here is a usage example:
Path file = ...
BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);
FileTime time = attrs.lastAccessTime();
For more information see Managing Metadata in the Java Tutorial.

You can't do it with plain Java, you'll need to use JNI to access the platform specific data such as this or use extensions to the core Java library like the following:
javaxt.io.File file = new javaxt.io.File("path");
file.getLastAccessTime();
Or, if you have Java 7, go with Esko's answer and use NIO.

Related

How can you load data dynamically with the FMPP Java API?

I am integrating a FMPP transformation into our Java code base. For this, I am using the FMPP Java API. For practical reasons, I have two separate directories:
one which contains the template: <absolute path template>/template.ftlx
one which contains the data: <absolute path data>/data.xml
This somehow complicates things, as I have to work with absolute paths here. Typically, the data is inside the template directory (together with a ignoredir.fmpp file). However, in our application, the data is coming from an external source (it is uploadable via a REST API), while the template is in the classpath. This also means that the data directory isn't static.
I am struggling to define all this and get the transformation happening via the Java API. Currently, I have the following:
Settings s = new Settings(new File("."));
s.set(Settings.NAME_SOURCES, new File("<absolute path template>/template.ftlx").getAbsolutePath());
s.set(Settings.NAME_OUTPUT_FILE, new File("<absolute path output>/output.xml").getAbsolutePath());
s.execute();
The code snippet above is not complete, as I have to add the data. There are the Settings.NAME_DATA and Settings.NAME_DATA_ROOT properties, but I can't get it working. I tried setting Settings.NAME_DATA_ROOT as following:
s.set(Settings.NAME_DATA_ROOT, new File("<absolute path data>").getAbsolutePath());
Then, I get the exception that FreeMarker cannot find my data:
The following has evaluated to null or missing:
==> d [in template "template.ftlx" at line 4, column 12]
In the template, I simply do:
<#list d.items>...</#list>
This makes sense that this would not work, as I did nowhere define that the data should be accessible via the d. hash (which I am doing below in config.fmpp). But I don't know how to define that properly via Settings.NAME_DATA and/or Settings.NAME_DATA_ROOT.
How can I inject my data file into all this? It should get the key d, so I can refer to d. in the template.
reference
Just as reference, if I create the following config.fmpp file in <absolute path config>, put the data.xml data file in directory <absolute path data> and call s.load(new File("<absolute path config>/config.fmpp")) before s.execute() above, everything is working fine.
data: {
d: xml(<absolute path data>/data.xml)
}
All I have to figure out is doing this in a dynamic fashion via the Java API. I cannot use config.fmpp for this, as the location of the data isn't static (and, as far as I know, config.fmpp is not parametrizable).
working solution, with doubts
After some code reading, I got it working if I do the following:
Settings s = new Settings(new File("."));
s.set(Settings.NAME_SOURCES, new File("<absolute path template>/template.ftlx").getAbsolutePath());
s.set(Settings.NAME_OUTPUT_FILE, new File("<absolute path output>/output.xml").getAbsolutePath());
s.set(Settings.NAME_DATA, "{d:xml(<absolute path output>/data.xml)}");
s.execute();
Here, we pass {configuration:xml(<absolute path output>/data.xml)} as a TDD to the NAME_DATA property. Is this the way to go? It "feels" strange to construct a textual definition in our code. Is there a way to do this in pure Java?

Avoid download of previously downloaded files using Java FTP

I have a desktop application which downloads all files on server.When a new file is added I want to download only the newer file.
Well to know which one is the "new one" you have to create a map/or other datastructure and put pair of the name/metadata creationtime(or last modified time)which one suits you best , when you iterate over your files just see their metadata with
Path file = ...;
BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class);
attr.creationTime(); //or attr.lastModifiedTime();
When you compare these times with one on server decide to download only the one with latest time.
Either way you have to keep track of at least the name/time modified(or created) at your previous download and compare these.
If this application on your desktop is not some kind of service that runs nonstop,find some way to persist that data on system,serialization or embed database h2/hsqldb within it.Use streams with conncurent iteration/ parralelStream to check these times and compare ,in case you use java8
edit- to get metadata from url, check this question Get the Last Modified date of an URL

How to check if the OS is POSIX compliant

I am writing a cross-platform application that creates temporary files and copies these to another location, where they need to be readable by everyone. (By default, only the owner has read access to temporary files.) I tried using the POSIX file permissions as follows:
FileAttribute<Set<PosixFilePermission>> attrs =
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rw-r--r--"));
Path temp = Files.createTempFile(null, ".tmp", attrs);
But this results in an exception on non-POSIX platforms:
java.lang.UnsupportedOperationException: 'posix:permissions' not supported as initial attribute
I want to add a simple check so that I can use the file permissions where necessary, without breaking compatibility with other platforms.
Digging deeper within JDK code, this is the check being used for POSIX
from java.nio.file.TempFileHelper
private static final boolean isPosix =
FileSystems.getDefault().supportedFileAttributeViews().contains("posix");
You can use this check.

Reading INI File in JAVA

I was trying to get values from an INI File and I'm writing the code in Java.
So far, I have read about this function and yes, it is returning values from the INI File.
String property = properties.getProperty("property.name");
But this is the code that I need.
Is there a function in Java that is equivalent to the GetPrivateProfileString of C?
No, there is no built-in initialization .INI file support in Java.
Java uses Properties files, which lays its data out like so:
propertyOne = 1
propertyTwo = 2
C uses Initialization files (.INI), which lays its data out in sections:
[Section]
propertyOne = 1
propertyTwo = 2
In Java, Properties don't have "sections", but can be divided up into sections by the same convention for naming packages:
section.propertyOne = 1
section.propertyTwo = 2
So you would call properties.getProperty("section.propertyOne");, where in INI you call the section first and then the property instead of as one.
INI files offer some more usefulness, but are not natively supported. A good 3rd party Java API for dealing with INI files is ini4j.
http://ini4j.sourceforge.net/
The reason for this is that .INI is a Windows-specific file. You won't really find .INI files on other operating systems, and Java is a cross-platform language.

Getting date/time of creation of a file

This seems like a pretty straightforward question but I haven't been able to find a definitive answer anywhere online. How can I get the date/time a file was created through Java's file manager? Aside from just the name of a file, what else can I get about the file's "properties"?
I'm not sure how you'd get it using Java 6 and below. With Java 7's new file system APIs, it'd look like this:
Path path = ... // the path to the file
BasicFileAttributes attributes =
Files.readAttributes(path, BasicFileAttributes.class);
FileTime creationTime = attributes.creationTime();
As CoolBeans said, not all file systems store the creation time. The BasicFileAttributes Javadoc states:
If the file system implementation does not support a time stamp to indicate the time when the file was created then this method returns an implementation specific default value, typically the last-modified-time or a FileTime representing the epoch (1970-01-01T00:00:00Z).

Categories

Resources