获取Linux/Unix文件系统信息

 设计一套SAP监控系统

http://blog.csdn.net/bayaci/archive/2009/10/13/4666456.aspx

显示系统df(linux )/ bdf (hpunix)的信息
获取Linux/Unix文件系统信息_第1张图片

执行代码,获取诸如 bdf /usr命令后的信息:
DiskInfo diskInfo = new DiskInfo();
diskInfo.info(directory);
long used = diskInfo.getUsed();
long free = diskInfo.getAvailable();
long kbyte = diskInfo.getKbyte();






代码如下:
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.StringTokenizer;
 
 
public class DiskInfo {

   /** OS line separator. */
     static final String NL = System.getProperty( "line.separator" );

   private static long getLong( String value ) throws NumberFormatException {
       try {
           return Long.parseLong( value );
       }
       catch( NumberFormatException nfe ) {
           throw new NumberFormatException( "On value "+value+": "+nfe.getMessage() );
       }
   }


   private String fileSystem ;
   private String mounted ;
private  long used = 0L;
private long kbyte = 0L;
private long available = 0L;

   public   void info(String directory  ) throws IOException {

      InputStream stream = null;
     // Connection connection = null; // Database Connection Object

 

 
         // to handle standard outputs
         final byte[] buf = new byte[1024];
         String[] strings;
         int i;
         int size;

 
         // note OS name
         final String osName = System.getProperty( "os.name" );

        // System.out.println( "system is  " + osName);
        
        
         if( "Linux".equals( osName ) ) {
            final Process p = Runtime.getRuntime().exec( "df " + directory );
            stream = p.getInputStream();
            final int count = stream.read( buf, 0, buf.length );

            String delim = NL;

            final StringTokenizer st = new StringTokenizer( new String( buf, 0, count ), delim,
true );

            int lineNum = 1;
            int field = 0;

            while( st.hasMoreTokens() ) {
                final String token = st.nextToken(delim);
                //System.out.println( "token is  " + token);
                if( lineNum < 2 ) {
                    if( NL.equals( token ) ) {
                        lineNum++;
                        if( lineNum == 2 ) {
                            delim = " " + NL;
                        }
                    }
                }// else
                if( !" ".equals( token ) ) {
                    field++;
                    if( field == 0 ) {
                        setFileSystem (   token  );
                    } else
                    if( field == 2 ) {
                        setUsed ( getLong( token ));
                    } else
                    if( field == 3 ) {
                        setAvailable( getLong( token ));
                    }
                }
            }
         }
         else
         if( "HP-UX".equals( osName ) ) {
            final Process p = Runtime.getRuntime().exec( "bdf " + directory );
            stream = p.getInputStream();
            final int count = stream.read( buf, 0, buf.length );

            String delim = NL;

            final StringTokenizer st = new StringTokenizer( new String( buf, 0, count ), delim,
true );

            int lineNum = 1;
            int field = 0;

            while( st.hasMoreTokens() ) {
                final String token = st.nextToken(delim);
                if( lineNum < 2 ) {
                    if( NL.equals( token ) ) {
                        lineNum++;
                        if( lineNum == 2 ) {
                            delim = " " + NL;
                        }
                    }
                } else
                if( !" ".equals( token ) ) {
                    field++;
                    
                   
                   
                    if( field == 0 ) {
                        setFileSystem (   token  );
                    } else
                    if( field == 3 ) {
                        setUsed ( getLong( token ));
                    } else
                    if( field == 4 ) {
                        setAvailable( getLong( token ));
                    }
                }
            }
         } else {
            throw new RuntimeException( "Unknown OS name: " + osName + " to implement..." );
         }
 
         setKbyte ( getUsed() + getAvailable());
         setMounted(directory );
 
 
   }

   private void setUsed(long used) {
    this.used = used;
}

public long getUsed() {
    return used;
}

private void setKbyte(long kbyte) {
    this.kbyte = kbyte;
}

public long getKbyte() {
    return kbyte;
}

private void setAvailable(long available) {
    this.available = available;
}

public long getAvailable() {
    return available;
}

private void setFileSystem(String fileSystem) {
    this.fileSystem = fileSystem;
}

public String getFileSystem() {
    return fileSystem;
}

private void setMounted(String mounted) {
    this.mounted = mounted;
}

public String getMounted() {
    return mounted;
}

 
 
  
  
}



你可能感兴趣的:(获取Linux/Unix文件系统信息)