C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

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,
        // this will get the owner information.
        UserPrincipal userPrincipal = view.getOwner();
        // print information.
        System.out.println("Owner of the file is :"  +userPrincipal.getName());


Enter the file path : E:/JAVA
Owner of the file is : DESKTOP-LP73A9B\INCLUDEHELP

You may also be interested in...

C/C++ Tips and Tricks...

Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.

Comments and Discussions