Java中如何得到某个远程机器的磁盘信息,包括有几个盘符,盘符已用空间及总空间,jdk1.6中可以的到本地磁盘空间信息,是否也能的到远程的呢?
import java.io.File;public class SpaceChecker
{
private long totalspace;
private long freespace;
private long usedspace;
private String ip;
private File[] roots;
public SpaceChecker()
{
totalspace=0;
freespace=0;
usedspace=0;
//ip="127.0.0.1";
roots = File.listRoots();
}
public SpaceChecker(String i)
{
//怎么得到IP为i的磁盘信息?
totalspace=0;
freespace=0;
usedspace=0;
ip=i;
File f=new File("\\\\192.168.126.119\\c$");
roots=f.listRoots();
}
public long GetAllTotalSpace()
{
for (File _file : roots) {
totalspace+=_file.getTotalSpace();
}
return totalspace;
}
public long GetAllFreeSpace()
{
for (File _file : roots) {
freespace+=_file.getFreeSpace();
}
return freespace;
}
public String[] GetDiskName()
{
String[] perdiskname;
perdiskname=new String[roots.length];
for (int i=0;i<roots.length;i++) {
perdiskname[i] =roots[i].getAbsolutePath();
}
return perdiskname;
}
public long[] GetTotalSpace()
{
long[] pertotalspace;
pertotalspace=new long[roots.length];
for (int i=0;i<roots.length;i++) {
pertotalspace[i] =roots[i].getTotalSpace();
}
return pertotalspace;
}
public long[] GetFreeSpace()
{
long[] perfreespace;
perfreespace=new long[roots.length];
for (int i=0;i<roots.length;i++) {
perfreespace[i] =roots[i].getFreeSpace();
}
return perfreespace;
}
public int GetNum()
{
return roots.length;
}
}
import java.io.File;public class SpaceChecker
{
private long totalspace;
private long freespace;
private long usedspace;
private String ip;
private File[] roots;
public SpaceChecker()
{
totalspace=0;
freespace=0;
usedspace=0;
//ip="127.0.0.1";
roots = File.listRoots();
}
public SpaceChecker(String i)
{
//怎么得到IP为i的磁盘信息?
totalspace=0;
freespace=0;
usedspace=0;
ip=i;
File f=new File("\\\\192.168.126.119\\c$");
roots=f.listRoots();
}
public long GetAllTotalSpace()
{
for (File _file : roots) {
totalspace+=_file.getTotalSpace();
}
return totalspace;
}
public long GetAllFreeSpace()
{
for (File _file : roots) {
freespace+=_file.getFreeSpace();
}
return freespace;
}
public String[] GetDiskName()
{
String[] perdiskname;
perdiskname=new String[roots.length];
for (int i=0;i<roots.length;i++) {
perdiskname[i] =roots[i].getAbsolutePath();
}
return perdiskname;
}
public long[] GetTotalSpace()
{
long[] pertotalspace;
pertotalspace=new long[roots.length];
for (int i=0;i<roots.length;i++) {
pertotalspace[i] =roots[i].getTotalSpace();
}
return pertotalspace;
}
public long[] GetFreeSpace()
{
long[] perfreespace;
perfreespace=new long[roots.length];
for (int i=0;i<roots.length;i++) {
perfreespace[i] =roots[i].getFreeSpace();
}
return perfreespace;
}
public int GetNum()
{
return roots.length;
}
}
解决方案 »
- 关于JAVA获取路径的问题,很奇怪
- 怎么使用异常控制循环
- String类型数据转换为byte再从byte转为String,为何这时数据前多了方形的空格?
- java类的功能太多,把功能分散给2个类,这个要怎么实现,或者用什么设设计模式?
- GUI里一个JFrame如何调用另一个JFrame??
- 求Jbuilder9.0哪里有下载啊!!!!!!!!!!!!!!
- getModel()和setModel()有什么用?
- 不能写成List<String,Double>吗?
- The complete java2 certification study guide 哪儿有下载的?
- 反射机制,它到底有什么用处?
- 请教:java有没有将日语汉字转化成振假名(日文:振り仮名,furigana)的函数?
- Hi,老紫竹,pls help ;use State.java read out the actual state from the split.java!!!!!!!!!!
package mainframe;
import java.io.File;public class SpaceChecker
{
private long totalspace;
private long freespace;
private long usedspace;
private String ip;
private File[] roots;
private String[] patharray;
public SpaceChecker()
{
totalspace=0;
freespace=0;
usedspace=0;
//ip="127.0.0.1";
roots = File.listRoots();
}
public SpaceChecker(String i)
{
totalspace=0;
freespace=0;
usedspace=0;
ip=i;
File f=new File("\\\\192.168.126.119\\c$");
roots=f.listRoots();
}
public SpaceChecker(String[] apatharray)
{
totalspace=0;
freespace=0;
usedspace=0;
patharray=apatharray;
//File f=new File("\\\\192.168.126.119\\c$");
//roots=f.listRoots();
}
public long GetAllTotalSpaceByPath()
{
try {
for (int i = 0; i < patharray.length; i++) {
String[] ipstr = patharray[i].split("------");
String ph = "\\\\"
+ ipstr[0]
+ "\\"
+ ((ipstr[1].replace(":", "")).replace("/", ""))
.replace("\\", "") + "$"; File f = new File(ph);
totalspace += f.getTotalSpace();
}
} catch (Exception e) {
e.printStackTrace();
}
return totalspace;
}
public long GetTotalSpaceByPath(String str)
{
File f=null;
try{
String[] ipstr=str.split("------");
String ph="\\\\"+ipstr[0]+"\\"+((ipstr[1].replace(":", "")).replace("/", "")).replace("\\", "")+"$";
f=new File(ph);
}catch(Exception e){e.printStackTrace();}
return f.getTotalSpace();
}
public long GetFreeSpaceByPath(String str)
{
File f=null;
try{
String[] ipstr=str.split("------");
String ph="\\\\"+ipstr[0]+"\\"+((ipstr[1].replace(":", "")).replace("/", "")).replace("\\", "")+"$";
f=new File(ph);
}catch(Exception e){e.printStackTrace();}
return f.getFreeSpace();
}
public long GetAllFreeSpaceByPath()
{
try {
for (int i = 0; i < patharray.length; i++) {
String[] ipstr = patharray[i].split("------");
String ph = "\\\\"
+ ipstr[0]
+ "\\"
+ ((ipstr[1].replace(":", "")).replace("/", ""))
.replace("\\", "") + "$"; File f = new File(ph);
freespace += f.getFreeSpace();
}
} catch (Exception e) {
e.printStackTrace();
}
return freespace;
}
public long GetAllTotalSpace()
{
for (File _file : roots) {
totalspace+=_file.getTotalSpace();
}
return totalspace;
}
public long GetAllFreeSpace()
{
for (File _file : roots) {
freespace+=_file.getFreeSpace();
}
return freespace;
}
public String[] GetDiskName()
{
String[] perdiskname;
perdiskname=new String[roots.length];
for (int i=0;i<roots.length;i++) {
perdiskname[i] =roots[i].getAbsolutePath();
}
return perdiskname;
}
public long[] GetTotalSpace()
{
long[] pertotalspace;
pertotalspace=new long[roots.length];
for (int i=0;i<roots.length;i++) {
pertotalspace[i] =roots[i].getTotalSpace();
}
return pertotalspace;
}
public long[] GetFreeSpace()
{
long[] perfreespace;
perfreespace=new long[roots.length];
for (int i=0;i<roots.length;i++) {
perfreespace[i] =roots[i].getFreeSpace();
}
return perfreespace;
}
public int GetNum()
{
return roots.length;
}
public static void main(String args[])
{
String[] strarray={"192.168.126.119------C:","192.168.126.119------D:"};
SpaceChecker sp=new SpaceChecker(strarray);
System.out.println(sp.GetAllTotalSpaceByPath());
}
}