Q:

Java program to get the file\'s owner name

belongs to collection: Java File Handling Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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,
      FileOwnerAttributeView.class);

    // this will get the owner information.
    UserPrincipal userPrincipal = view.getOwner();

    // print information.
    System.out.println("Owner of the file is :" + userPrincipal.getName());
  }
}

Output

Enter the file path : E:/JAVA
Owner of the file is : DESKTOP-LP73A9B\NERDUTELLA

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java File Handling Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to get file creation, last access and... >>
<< Java program to get the basic file attributes (spe...