Problem Statement
Write a command line tool which takes a file path as input and prints the number of lines, number of words, number of characters in the file, userid of the owner of the file, groupid of owner of the file and last modification time of the file in UNIX
timestamp format. Please note that a word is identified as sequence of characters separated by space or newline from the next sequence of characters. Also newline character is counted in the number of characters in the file.
It has to be written in java.
My code:
try {
File f = new File("src/test.txt");
BufferedReader br = new BufferedReader(new FileReader(f));
String line = br.readLine();
int lines = 0, words = 0, chars = 0;
while (line != null) {
lines++;
for(int i = 0; i < line.length(); i++) {
if(line.charAt(i)==' ') {
words++;
}
}
chars += line.length();
line = br.readLine();
words++;
}
Path path = Paths.get("src/test.txt");
long d2 = Files.getLastModifiedTime(path, LinkOption.NOFOLLOW_LINKS).toMillis();
int uid = (Integer)Files.getAttribute(path, "unix:uid");
System.out.println(lines);
System.out.println(words);
System.out.println(chars);
System.out.println(uid);
System.out.println(uid);
System.out.println(d2);
}
catch(Exception e) {
e.printStackTrace();
}
The problem I am facing is how to find out user id and group id of the owner. I am getting run time error when I used the above code
java.lang.UnsupportedOperationException: View 'unix' not available
at sun.nio.fs.AbstractFileSystemProvider.readAttributes(Unknown Source)
at java.nio.file.Files.readAttributes(Unknown Source)
at java.nio.file.Files.getAttribute(Unknown Source)
at Demo.main(Demo.java:31)
Also after completing my code I would like to submit it but when I submit my code I get a compile time error:
import java.nio.file cannot be resolved
and similarly for others too that belongs to same package, so I would also like to know is there any other method to get these properties where my code gets accepted?