Open In App

Java Program to Get the File’s Owner Name

Last Updated : 28 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The File class is Java’s representation of a file or directory pathname. Because file and directory names have different formats on different platforms, an easy string isn’t capable name them. The File class contains several methods for working with the pathname, deleting and renaming files, creating new directories, listing the contents of a directory, and determining several common attributes of files and directories.

  • It is an abstract representation of file and directory path names.
  • A pathname, whether abstract or in string form are often either absolute or relative. The parent of an abstract pathname could also be obtained by invoking the getParent() method of this class.
  • First, we should always create the File class object by passing the filename or directory name. A file system may implement restrictions to certain operations on the particular file-system object, like reading, writing, and executing. These restrictions are collectively referred to as access permissions
  • Instances of the File class are immutable; that’s, once created, the abstract pathname represented by a File object will never change.

To find the file owner in Java we will use getOwner() method of the FileOwnerAttributeView class.

Approach:

  • Take the file’s path as input.
  • Create an object with file attribute using FileOwnerAttributeView class.
  • Then use the getOwner() method to get the owner’s name.
  • Print the file owner’s name.

Syntax:

file_attribute_object.getOwner()

Parameter: It can be used only on objects which have the file attributes with them.

Return Type: It returns the file owner’s name.

Example 1: In Windows OS

Java




// Importing modules
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.*;
  
class GFG {
    public static void main(String[] args) {
  
        // Taking file path as input
        Path path = Paths.get("C:\\Users\\Gfg\\Article 1.txt");
  
        // Create object having the file attribute
        FileOwnerAttributeView file = Files.getFileAttributeView(path, 
                                        FileOwnerAttributeView.class);
  
        // Exception Handling to avoid any errors
        try {
            // Taking owner name from the file
            UserPrincipal user = file.getOwner();
  
            // Printing the owner's name
            System.out.println("Owner: " + user.getName());
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}


Output

Owner: DESKTOP-O30NR1H\Aditya_Taparia

If we do not use exceptional handling while using getOwner() method then it will display an error.

Output:

gfg.java:16: error: unreported exception IOException; must be caught or declared to be 
thrown
                UserPrincipal user = file.getOwner();
                                                  ^
1 error

Example 2: In the Linux Machine(Ubuntu Distribution)

Java




// Importing modules
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.*;
  
public class GFG {
    public static void main(String[] args) {
  
        // Taking file path as input
        Path path = Paths.get("/home/ganesh/GFG.java");
  
        // Create object having the file attribute
        FileOwnerAttributeView file = Files.getFileAttributeView(path, 
                                        FileOwnerAttributeView.class);
  
        // Exception Handling to avoid any errors
        try {
            // Taking owner name from the file
            UserPrincipal user = file.getOwner();
  
            // Printing the owner's name
            System.out.println("Owner: " + user.getName());
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}


Output:

get file owner in linux os



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads