wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes getting unix like file information the java way Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "getting unix like file information the java way" Watch "getting unix like file information the java way" New topic
Author

getting unix like file information the java way

Dieter Merlin
Greenhorn

Joined: Oct 25, 2005
Posts: 9
What I basically want, is to find out wheter the owner, group and others have read, write and exec permissions on a file as well as the owner/group of that file (or directory).

So far I do not know if there is any way to do so in Java, so if there isnt, is there a way then to get the result of an execution of ls -l as a String ?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,

Welcome to JavaRanch!

Java doesn't have the concept of user/group/others, as UNIX does. There are some rudimentary methods you can call in the java.io.File class to get some information about the current user's effective permissions on a file, but that's about it.

You can run ls -l, or any other command, using the java.lang.Runtime.exec() family of methods.


[Jess in Action][AskingGoodQuestions]
Dieter Merlin
Greenhorn

Joined: Oct 25, 2005
Posts: 9
hmm, and there are no frameworks available to somehow listen on those Runtime.exec() calls to get their output ?
Qt (c++) has such implementation but I have no knowledge of that in Java

I also looked at commons-io but that didnt have what I needed, sadly
[ October 25, 2005: Message edited by: Dieter Merlin ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

You call Runtime.exec(), and you get a java.lang.Process. You call getInputStream() on the Process, and you get a java.io.InputStream. You can then read the process's output from this InputStream. The Process also gives you a stream to read standard error, and a stream that you can write to to provide input for the process.
Dieter Merlin
Greenhorn

Joined: Oct 25, 2005
Posts: 9
aah
thanks, i got it working now.

this is perfect, now I can get the info line per line
Guy Allard
Ranch Hand

Joined: Nov 24, 2000
Posts: 776
Dieter - FYI, there are some pitfalls with Runtime.exec().

All is explained here:

RuntimeExec

Guy
Dieter Merlin
Greenhorn

Joined: Oct 25, 2005
Posts: 9
Wel, I got a lot of things working with it, but for some reason, it refuses to get the information from files or directories with a space in between.

The following command :
ls -l "/some/dir/AC Seven Yearmix 2004.mp3"
would provide me hte proper info, as the quotes take care of the spaces.
If I try to implement this in the Runtime.exec() I get nothing.
Runtime.getRuntime().exec( "ls -l \"/some/dir/AC Seven Yearmix 2004.mp3\"" ).getImputStream();

gives an empty string after reading all its bytes.
Reason: for some unknown reason, the exec method considers Seven, Yearmix etc as other arguments and as such the command of course cannot find the file

if there some way to escape a space or to replace it with some ascii character to make sure it is preserved as one string?

edit: found it.
if I do a myFile.getName().replace(' ', (char) Character.DIRECTIONALITY_WHITESPACE)

it is fixed
[ October 28, 2005: Message edited by: Dieter Merlin ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: getting unix like file information the java way
 
Similar Threads
Error starting Tomcat in Ubuntu
Java code for giving write permission to group(775)
can a user find his permissions
File Owner
Confusion in chown unix commands