In this example, I show you how to get the owner of the file in Java using Files.getOwner() API.
java.nio.file.Files.getOwner() API
This method returns the owner of a file.
The path parameter is associated with a file system that supports FileOwnerAttributeView.
This file attribute view provides access to a file attribute that is the owner of the file.
Java Files.getOwner API Example
In the example, we get the owner of the file in Java.
package net.javaguides.corejava.io;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.UserPrincipal;
public class JavaGetFileOwner {
public static void main(String[] args) throws IOException {
Path myPath = Paths.get("src/sample.txt");
UserPrincipal userPrincipal = Files.getOwner(myPath);
String owner = userPrincipal.getName();
System.out.println(owner);
}
}
Output:
Ramesh\CM-1787
Reference
Related File Handling Examples
- How to Create a New File in Java
- How to Write to File in Java
- How to delete a file in Java
- How to Copy the File in Java
- How to Move a File in Java
- How to Rename the File in Java
- How to Append to File in Java
- Read the File using BufferReader
- Read File using BufferInputStream
- Read the File using DataInputStream
- Reading a File in Java using BufferedInputStream
- Read a File in Java using BufferedReader
- Java Get File Extension From Filename
- Get File Size in Bytes KB MB GB TB
- How to Check if a File is Empty in Java
Comments
Post a Comment
Leave Comment