java 得到磁盘的可用空间

来源:互联网 发布:数据库查询语句select 编辑:程序博客网 时间:2024/04/27 20:10

import java.util.*;
import java.io.*;

public class DiskUtil {

  public static long getFreeSpace(String path) throws Exception {
    return getFreeSpace(path, "GBK");
  }

  public static long getFreeSpace(String path, String charset) throws Exception {
    if (System.getProperty("os.name").startsWith("Windows")) {
      return getFreeSpaceOnWindows(path, charset);
    }
    if (System.getProperty("os.name").startsWith("Linux")) {
      return getFreeSpaceOnLinux(path);
    }

    throw new UnsupportedOperationException(
        "The method getFreeSpace(String path) has not been implemented for this operating system.");
  }

  private static long getFreeSpaceOnWindows(String path, String charset) throws Exception {
    long bytesFree = -1;

    File script = new File(System.getProperty("java.io.tmpdir"), "script.bat");
    PrintWriter writer = new PrintWriter(new FileWriter(script, false));
    writer.println("dir /"" + path + "/"");
    writer.close();

    // get the output from running the .bat file
    Process p = Runtime.getRuntime().exec(script.getAbsolutePath());
    InputStream reader = new BufferedInputStream(p.getInputStream());
    StringBuffer buffer = new StringBuffer();
    for (;;) {
      int c = reader.read();
      if (c == -1)
        break;
      buffer.append((char) c);
    }
    String outputText = buffer.toString();
    reader.close();

    outputText = new String(outputText.getBytes("ISO-8859-1"), charset);

    // parse the output text for the bytes free info
    StringTokenizer tokenizer = new StringTokenizer(outputText, "/n");
    while (tokenizer.hasMoreTokens()) {
      String line = tokenizer.nextToken().trim();
      // see if line contains the bytes free information
      if (line.endsWith("bytes free")) {
        tokenizer = new StringTokenizer(line, " ");
        tokenizer.nextToken();
        tokenizer.nextToken();
        bytesFree = Long.parseLong(tokenizer.nextToken().replaceAll(",", ""));
      } else if (line.endsWith("可用字节")) {
        String[] parts = line.split(" ");
        bytesFree = Long.parseLong(parts[2].replaceAll(",", ""));
      }
    }
    return bytesFree;
  }

  private static long getFreeSpaceOnLinux(String path) throws Exception {
    long bytesFree = -1;

    Process p = Runtime.getRuntime().exec("df " + "/" + path);
    InputStream reader = new BufferedInputStream(p.getInputStream());
    StringBuffer buffer = new StringBuffer();
    for (;;) {
      int c = reader.read();
      if (c == -1)
        break;
      buffer.append((char) c);
    }
    String outputText = buffer.toString();
    reader.close();

    // parse the output text for the bytes free info
    StringTokenizer tokenizer = new StringTokenizer(outputText, "/n");
    tokenizer.nextToken();
    if (tokenizer.hasMoreTokens()) {
      String line2 = tokenizer.nextToken();
      StringTokenizer tokenizer2 = new StringTokenizer(line2, " ");
      if (tokenizer2.countTokens() >= 4) {
        tokenizer2.nextToken();
        tokenizer2.nextToken();
        tokenizer2.nextToken();
        bytesFree = Long.parseLong(tokenizer2.nextToken());
        return bytesFree;
      }

      return bytesFree;
    }

    throw new Exception("Can not read the free space of " + path + " path");
  }

  public static void main(String args[]) {
    try {
      System.out.println("Free space of /: " + getFreeSpace(""));
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}
 

// 调用代码

 public static void main(String[] args) throws Exception {
    System.out.println(DiskUtil.getFreeSpace("d://"));
  }

原创粉丝点击