Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting file creator/owner attributes in Java

Tags:

java

posix

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?

like image 931
Paul J Abernathy Avatar asked Dec 16 '14 20:12

Paul J Abernathy


People also ask

What are file attributes in Java?

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.

What is file metadata in Java?

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.

How do you read write a text file in Java?

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.


1 Answers

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.

like image 176
Elliott Frisch Avatar answered Sep 30 '22 20:09

Elliott Frisch