JAVA 如何获取硬盘的ID,在网上找到了都是C#和VB.net 或者DELPHI以及其他的,很郁闷??
JSP软件如何防止拷贝和盗版呢
JSP软件如何防止拷贝和盗版呢
解决方案 »
- 聊天程序求大神解惑
- List的removeAll方法异常?
- 【100分】java的p2p实现,有人搞过吗
- SQL能直接操作本地XML吗?
- jdbc更新停顿问题。orcale9i,使用ojdbc14.jar
- j2EE,J2SE,J2ME之间的区别
- JTree 删除节点后用 updateUI()刷新出现异常! 急!!
- 有关repaint的问题
- help----from c++ to java...
- 高分求解:Class.forName("com.inet.tds.TdsDriver").newInstance();
- ~~~~~100分求一个非智能的两人对战的简单五子棋程序~~~~~~?
- 急!!!求助!!java下怎么能取得本地的cpu序号或硬盘的id号????
char diskBegin = 'c';
Vector diskList = new Vector();
try {
File f = new File(diskBegin + ":/");
while (f.exists()) {
diskList.add(diskBegin + "");
diskBegin = (char) (diskBegin + 1);
f = new File(diskBegin + ":/");
}
} catch (Exception e) {
System.out.println("exception~~~");
e.printStackTrace();
}
return diskList;
}
一个获得本地所有盘符的不太简便的方法~~~~
public static Vector getDisk() {
char diskBegin = 'c';
Vector diskList = new Vector();
try {
File f = new File(diskBegin + ":/");
while (f.exists()) {
System.out.println(diskBegin + ":/" + f.canWrite());
if(f.canWrite()){
diskList.add(diskBegin + "");
}
diskBegin = (char) (diskBegin + 1);
f = new File(diskBegin + ":/");
}
} catch (Exception e) {
System.out.println("exception~~~");
e.printStackTrace();
}
return diskList;
}
C:/TRUE >...
-------------------------------------------------
楼上的两位给的是取硬盘id的方法吗?
-------------------------------------------------我可没说我写的是取硬盘id的方法,希望shenpipi(皮皮) 能在下次的发言前,仔细的浏览一下,不要妄加评论~~~
for (int i = 0; i < root.size(); i++) {
System.oput.println(root.get(i).toString());
}你可以在你的main()方法中写上述代码进行测试,看是否满足你的要求~~~
输出的是测试信息。
F:/false等(false标记)为非硬盘,没向Vector中加
True标记的为硬盘,加到了Vector中。
显示Vector内容可按楼上的方法。
Process ps=null;
try
{
ps=execpro.exec("程序");
ps.waitFor();
}
只要使用File的static 方法
File[] listRoots( )就可以了。
何必要自己去写函数,
不过可能没有区别光驱。
呵呵,没错,你是说的一个获得本地所有盘符的不太简便的方法~~~~
但是,楼主要的是什么?你给的是什么?我觉得奇怪,随便问一句.嘿嘿.
我这如果是妄加评论的话,你那答非所问又算什么?
import java.util.*;
import java.util.regex.*;public class HardWareID {
String IPCONFIG_COMMAND_WIN = "ipconfig /all";
boolean realMac = true;
String unique = ""; public static String getMacAddress() {
HardWareID hwid = new HardWareID();
return hwid.getUnique().trim();
} private String getUnique() {
String os = System.getProperty("os.name"); if (os.startsWith("Windows")) {
return getUniqueWindows();
}else {
return "";
}
} private String getUniqueWindows() {
// 运行控制台命令,返回结果字符串
String ipConfigResponse = null;
try {
ipConfigResponse = runConsoleCommand(IPCONFIG_COMMAND_WIN);
}
catch (IOException e) {
e.printStackTrace();
} // 按行分割结果字符串,并循环判断每个字符串直道找出 Mac 地址
StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");
while (tokenizer.hasMoreTokens()) {
String line = tokenizer.nextToken().trim(); // 获取每行 ":" 后的字符串
int macAddressPosition = line.indexOf(":");
if (macAddressPosition <= 0) {
continue;
}
String macAddressCandidate = line.substring(macAddressPosition + 1).
trim();
// 检查 macAddressCandidate 中内容是否为真实网卡 Mac 地址,根据 Mac 地址计算出唯一标识。
if (isMacAddWin(macAddressCandidate)) {
if (realMac == true) {
generateUnique(macAddressCandidate);
}
else {
realMac = true;
}
}
} return unique;
}
/**
* 运行控制台命令,返回结果字符串
* @param command String
* @return String
* @throws IOException
*/
private String runConsoleCommand(String command) throws IOException {
Process p = Runtime.getRuntime().exec(command);
InputStream stdoutStream = new BufferedInputStream(p.getInputStream()); StringBuffer buffer = new StringBuffer();
while (true) {
int c = stdoutStream.read();
if (c == -1) {
break;
}
buffer.append( (char) c);
}
String outputText = buffer.toString(); stdoutStream.close(); return outputText;
} /**
* 对输入参数进行检查,符合正则表达式的为 windows 平台下有效 Mac 地址
* @param macAddressCandidate String
* @return boolean
*/
private boolean isMacAddWin(String macAddressCandidate) {
Pattern macPattern = Pattern.compile("[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}");
Matcher m = macPattern.matcher(macAddressCandidate);
return m.matches();
} /**
* 对输入参数进行检查,符合长度的为 MAC OS X 下有效网卡 Mac 地址
* @param macAddressCandidate String
* @return boolean
*/
private boolean isMacAddOSX(String macAddressCandidate) {
if (macAddressCandidate.length() != 17) {
return false;
}
else {
return true;
}
} /**
* 产生 Unique
* @param macAddress String
*/
private void generateUnique(String macAddress) {
if (unique == "") {
unique += macAddress;
}
else {
unique += "#";
unique += macAddress;
}
} public static void main(String[] args) {
System.out.println(HardWareID.getMacAddress());
}
}这个方法是windows平台下通过运行控制台命令取网卡id。其实最好的方法还是使用c直接取硬件id,然后使用jni调用c的方法。另外,不是所有的硬盘,CPU都有序列号。如果要作为机器的唯一标识,建议组合使用。