I am trying to read in a list of files and find the user who created the file. With a *nix system, you can do something like
Map<String, Object> attrs = Files.readAttributes(Paths.get(filename), "posix:*");
However, when attempting it on a Windows system, I get an error because Windows is not able to access the POSIX properties. You can get the "regular" (non POSIX) properties by doing this:
attrs = Files.readAttributes(Paths.get(filename), "*");
But the file's creator is not included in that list.
Is there any way to find out who created the file in a Java program running on Windows?
A file attribute view that supports reading or updating a file's Access Control Lists (ACL) or file owner attributes. AttributeView. An object that provides a read-only or updatable view of non-opaque values associated with an object in a filesystem. BasicFileAttributes.
public abstract class FileMetadata extends java.lang.Object. This class represents a single file stored by the local file service. This class is abstract. There are concrete subclasses corresponding to each of the concrete backend storage repositories.
There are several ways to read a plain text file in Java e.g. you can use FileReader, BufferedReader, or Scanner to read a text file. Every utility provides something special e.g. BufferedReader provides buffering of data for fast reading, and Scanner provides parsing ability. Methods: Using BufferedReader class.
I believe you can use Files.getOwner(Path, LinkOption...)
to get the current owner (which may also be the creator) like
Path path = Paths.get("c:\\path\\to\\file.ext");
try {
UserPrincipal owner = Files.getOwner(path, LinkOption.NOFOLLOW_LINKS);
String username = owner.getName();
} catch (IOException e) {
e.printStackTrace();
}
This should work if it is a file system that supports
FileOwnerAttributeView
. This file attribute view provides access to a file attribute that is the owner of the file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With