
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get File Owner's Name in Java
As per the problem statement, we are going to find the file owner name in Java. The owner's name here represents the owner of the PC in which file is being runned. To find the file owner in Java we will use the getOwner() method of the FileOwnerAttributeView class.
FileOwnerAttributeView belongs to the java.nio class.
Let's deep dive into this article, to know how it can be done by using Java programming language.
To show you some instances
Instance-1
Suppose the given file location is "D:/saket.txt".
After checking for Owner name of the given file, the output will be ?
The Owner of the file is: LAPTOP-9IH5RA2V\SAKET
Instance-2
Suppose the given file location is "C:/Users/SAKET/Desktop/saket/Work".
After checking for Owner name of the given file, the output will be ?
The Owner of the file is: LAPTOP-9IH5RA2V\SAKET
Algorithm
Step 1 ? Take the file path as input.
Step 2 ? Create an object having the file attribute using FileOwnerAttributeView class.
Step 3 ? Declare try and catch the block to avoid any errors.
Step 4 ? Extract the owner name from the file using the getOwner() method.
Step 5 ? Print the result.
Syntax
getOwner() ? It is used to return or get the current owner of the given file. It belongs to FileOwnerAttributeView class in java.
Below is the syntax of it
file_attribute_object.getOwner()
Multiple Approaches
We have provided the solution in different approaches.
By Using Static Input.
By Using User Defined Method
Let's see the program along with its output one by one.
Note ? Executing these programs on any online editor may not give you the correct output. As online Java editors can not access your local system's file. So to get the expected output, you are suggested to run these programs in your local Java editor which is present in your system.
Approach-1: By Using Static Input
In this approach, the file path will be taken as a static input. Then as per the algorithm get the file owner's name.
Example
import java.io.*; import java.nio.file.*; import java.nio.file.attribute.*; public class Main { //main method public static void main(String[] args) { // Taking file path as input Path path = Paths.get("D:/saket.txt"); // Create object having the file attribute FileOwnerAttributeView file = Files.getFileAttributeView(path, FileOwnerAttributeView.class); // Declaring try and catch block to avoid any errors try { // Extracting owner name from the file UserPrincipal user = file.getOwner(); // Printing the owner's name as a result System.out.println("The Owner of the file is: " + user.getName()); } //catch block catch (Exception e){ System.out.println(e); } } }
Output
java.nio.file.NoSuchFileException: D:/saket.txt
Approach-2: By Using User Defined Method
In this approach, file path will be taken as input. Then call a user defined method by passing this file path as parameter andas per the algorithm get the file owner's name.
Example
import java.io.*; import java.nio.file.*; import java.nio.file.attribute.*; public class Main{ //main method public static void main(String[] args) { // calling user defined method func(); } //user defined method static void func() { // Taking file path as input Path path = Paths.get("C:/Users/SAKET KASHYAP/Desktop/saket/Work"); // Create object having the file attribute FileOwnerAttributeView file = Files.getFileAttributeView(path,FileOwnerAttributeView.class); // Declaring try and catch block to avoid any errors try { // Extracting owner name from the file UserPrincipal user = file.getOwner(); // Printing the owner's name as a result System.out.println("The Owner of the file is: " + user.getName()); } //catch block catch (Exception e){ System.out.println(e); } } }
Output
java.nio.file.NoSuchFileException: C:/Users/SAKET KASHYAP/Desktop/saket/Work
In this article, we explored different approaches to check the file owner's name by using Java programming language.