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

Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com (2015-2018), Some rights reserved.