Read file attributes (on UNIX filesystem)?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Read file attributes (on UNIX filesystem)?

  1. #1
    Join Date
    Nov 2004
    Location
    Colorado
    Posts
    2

    Read file attributes (on UNIX filesystem)?

    I am writing a "filesystem scanner" program that will collect statistics about files on a filesystem. I am using the java.io.File class, but the File class does not allow me to read File Access Timestamp. There is a lastModified() method, but there is no lastAccessed() method. Can anybody suggest on how to resolve this?

    Thanks,
    2FastDre.

  2. #2
    Join Date
    May 2004
    Location
    Durham, UK
    Posts
    174
    Hi,

    The only thing that I can think of is to run a java system method which carries out an 'ls' on the file, and then decode the output of that 'ls' to get the relevant timestamp.

    Hope this helps.
    Graham

  3. #3
    Join Date
    Nov 2004
    Location
    Colorado
    Posts
    2
    Could you provide a little more info or a small example on how to execute Java system commands, such as 'ls'?

    Thanks!
    Andre.

  4. #4
    Join Date
    May 2004
    Location
    Durham, UK
    Posts
    174
    Hi Andre,

    here is some code which runs on Windows XP unfortuately I don't have unix or linix so cannot test it on that, but theoretically you should just be able to replace the "cmd /c dir" with "ls -u afile.txt" where afile.txt is the file you want the access time from.


    Code:
    package test;
    
    import java.io.*;
    
    public class TestExecute
    {
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args)
        {
            try
            {
                 Runtime rt = Runtime.getRuntime();
                 Process p = rt.exec("cmd /c dir");
           
                 InputStream in = p.getInputStream();
                 OutputStream out = p.getOutputStream();
                 InputStream err = p.getErrorStream();
                 int intread = in.read();
                 char x;
                 while(intread!=-1)
                 {
                     x = (char)intread;
                     System.out.print( x + "");
                     intread = in.read();
                 }
    
            }catch(Exception exc){/*handle exception*/}
            
        }
        
    }
    Hope this helps.
    Graham

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center