This week's giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP and have Lucian Maly on-line!
See this thread for details.
Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

getting unix like file information the java way

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Dieter Merlin
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
aah
thanks, i got it working now.

this is perfect, now I can get the info line per line
 
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dieter - FYI, there are some pitfalls with Runtime.exec().

All is explained here:

RuntimeExec

Guy
 
Dieter Merlin
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
I can't take it! You are too smart for me! Here is the tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic