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
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.*;
class GFG {
public static void main(String[] args) {
Path path = Paths.get( "C:\\Users\\Gfg\\Article 1.txt" );
FileOwnerAttributeView file = Files.getFileAttributeView(path,
FileOwnerAttributeView. class );
try {
UserPrincipal user = file.getOwner();
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
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.*;
public class GFG {
public static void main(String[] args) {
Path path = Paths.get( "/home/ganesh/GFG.java" );
FileOwnerAttributeView file = Files.getFileAttributeView(path,
FileOwnerAttributeView. class );
try {
UserPrincipal user = file.getOwner();
System.out.println( "Owner: " + user.getName());
} catch (Exception e) {
System.out.println(e);
}
}
}
|
Output:

Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
28 Dec, 2020
Like Article
Save Article