-1

I was wondering if I can get an output like this with Java

enter image description here

That has to work under Linux, Windows and Mac; and if it's possible I prefer not to use Runtime.exec().

6
  • 2
    The methods are different for Linux, Windows, and Mac.
    – Alex W
    Oct 19, 2012 at 15:45
  • Are you okay with writing JNI? It seems like a lot more debt to build JNI for three (at least) platforms than to just call Runtime.exec... Oct 19, 2012 at 15:46
  • 2
    Well, write your own program in Java, using only portable code, that displays the output as desired. That should be easy using the new java.nio.file package Oct 19, 2012 at 15:46
  • @EdwinDalorzo can you please give me an example?
    – BackSlash
    Oct 19, 2012 at 15:50
  • @Harlandraka Java Tutorial NIO 2. Oct 19, 2012 at 15:51

2 Answers 2

1

I think you can use the metadata of a File object to get anything you need. Check the following link:

http://docs.oracle.com/javase/tutorial/essential/io/fileAttr.html

Example:

Path file = ...;
BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class);

System.out.println("creationTime: " + attr.creationTime());
System.out.println("lastAccessTime: " + attr.lastAccessTime());
System.out.println("lastModifiedTime: " + attr.lastModifiedTime());

(This example was taken from the link I provided).

Hope this helps you


Checking the Java API, I find out that the data you need (size, attributes, etc.) are accesible through the File class methods. Check: http://docs.oracle.com/javase/7/docs/api/java/io/File.html

Notice that linux and mac are both Unix-like systems, and windows is not. Therefore, you won't be able to get all the file attributes you can expect to get on linux or mac in windows... but you can get the most important values

4
  • Ok thanks but what about getting file permissions, owner, user, group?
    – BackSlash
    Oct 19, 2012 at 15:58
  • @Harlandraka check the link I provided; it has a section titled "POSIX File Permissions". But you must be aware that Windows is not POSIX
    – Barranka
    Oct 19, 2012 at 16:02
  • Thanks, this was helpful, now i have only another question, how do i format permissions to have output similar to ls -la? i.e. -rw-------
    – BackSlash
    Oct 19, 2012 at 16:51
  • @Harlandraka append your values to a string... format them any way you want (maybe using the java.text.NumberFormat class) and concatenate them
    – Barranka
    Oct 19, 2012 at 20:18
1

No, you can't, because the output you want has *nix-only attributes "owner" and "group", which don't exist on Windows platforms. A java File doesn't have this information and that would be the only way to get file info without using Runtime.exec()

3
  • 1
    Well ok but for example FileZilla Server works only on Windows and it emulates linux to get file permissions, there must be a way to do that, maybe with JNI or maybe not, but ther must be a way to do that... Any suggestions?
    – BackSlash
    Oct 19, 2012 at 15:57
  • You wrote the question... if you don't want to use Runtime.exec() you can't get it. If it were me, I'd detect the system type then run the appropriate command for the platform through Runtime.exec() and get what I need from the output
    – Bohemian
    Oct 19, 2012 at 16:10
  • Runtime.exec is slow and i think it's not the proper way to do it, because Java is not platform dependent so there must be a way to do it, i'll check the solution that Barranka gave me.
    – BackSlash
    Oct 19, 2012 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.