java获取磁盘空间_java 得到磁盘的可用空间 | 学步园

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://"));

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值