Home »
Java Programs »
Java File Handling Programs
Java program to get the file's owner name
In this java program, we are going to learn how to get the file's owner name? Method getOwner() of class FileOwnerAttributeView is used to get the owner name.
Submitted by IncludeHelp, on November 07, 2017
Given a file and we have to get, print the file's owner name.
Following packages are using here, to implement this program,
- java.nio.file.*
- java.nio.file.attribute.*
There are following two important classes, which are using this program to get the file's owner name.
- FileOwnerAttributeView
- UserPrincipal
The method getOwner() gives the owner's name to the object of UserPrincipal class, which can be accessed through getName() method.
Program to get owner's name of a file in java
import java.nio.file.*;
import java.nio.file.attribute.*;
import java.util.Scanner;
public class OwnerOfFile {
public static void main(String[] args) throws Exception {
// create object of scanner.
Scanner KB = new Scanner(System.in);
// enter path here.
System.out.print("Enter the file path : ");
String A = KB.next();
Path path = Paths.get(A);
// create object of file attribute.
FileOwnerAttributeView view = Files.getFileAttributeView(path,
FileOwnerAttributeView.class);
// this will get the owner information.
UserPrincipal userPrincipal = view.getOwner();
// print information.
System.out.println("Owner of the file is :" + userPrincipal.getName());
}
}
Output
Enter the file path : E:/JAVA
Owner of the file is : DESKTOP-LP73A9B\INCLUDEHELP
Java File Handling Programs »