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.
- 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.
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
If we do not use exceptional handling while using getOwner() method then it will display an error.
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)
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.