หน้าเว็บ

วันจันทร์ที่ 16 กันยายน พ.ศ. 2556

java 7 show disk space


maven dependencies (pom.xml)
    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
            <type>jar</type>
        </dependency>
    </dependencies>

    ....
    ....
    ....
DiskSpace.java
package com.blogspot.na5cent.diskspace;

import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.FileSystemException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import org.apache.commons.io.FileUtils;

/**
 * @author redcrow
 */
public class DiskSpace {

    public static void main(String[] args) throws IOException {
        for (Path root : FileSystems.getDefault().getRootDirectories()) {
            System.out.println(root);

            try {
                FileStore store = Files.getFileStore(root);
                long totalSpace = store.getTotalSpace();
                long freeSpace = store.getUsableSpace();
                long usedSpace = totalSpace - freeSpace;
                
                System.out.println("free space = " + diskSize(freeSpace));
                System.out.println("used space = " + diskSize(usedSpace));
                System.out.println("total space = " + diskSize(totalSpace));
            } catch (FileSystemException ex) {
                System.out.println("FileSystemException " + ex.getMessage());
            }
            
            System.out.println("===================================================");
        }
    }
    
    private static String diskSize(long size){
        return FileUtils.byteCountToDisplaySize(size);
    }
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น