转存

原创 2015年07月11日 00:51:45
package com.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Command {
	public static void exeCmd(String commandStr) {
		BufferedReader br = null;
		try {
			Process p = Runtime.getRuntime().exec(commandStr);
			br = new BufferedReader(new InputStreamReader(p.getInputStream(),"gbk"));
			String line = null;
			StringBuilder sb = new StringBuilder();
			while ((line = br.readLine()) != null) {
				sb.append(line + "\n");
				System.out.println(sb.toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		finally
		{
			if (br != null)
			{
				try {
					br.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void bbbb() {
		try {  
		    Runtime rt = Runtime.getRuntime();  
		    Process proc = rt.exec("javac");  
		    int exitVal = proc.waitFor();  
		    System.out.println("Process exitValue: " + exitVal);    
		} catch (Throwable t) {  
		    t.printStackTrace();  
		} 
	}
	
	public static void cccc() {
		BufferedReader br = null;
		try {  
			//String [] cmd={"cmd","/C","copy exe1 exe2"};
			String [] cmd={"diskpart","list disk"}; 
			Process p = Runtime.getRuntime().exec(cmd);
			br = new BufferedReader(new InputStreamReader(p.getInputStream(),"gbk"));
			String line = null;
			StringBuilder sb = new StringBuilder();
			while ((line = br.readLine()) != null) {
				sb.append(line + "\n");
				System.out.println(sb.toString());
			}
		} catch (Throwable t) {  
		    t.printStackTrace();  
		} 
	}
	
	public static void dddd() {
		try {  
		    Runtime rt = Runtime.getRuntime();  
		    String [] cmd={"diskpart","list disk"}; 
		    Process proc = rt.exec(cmd);  
		    InputStream stderr = proc.getErrorStream();  
		    InputStreamReader isr = new InputStreamReader(stderr,"GBK");  
		    BufferedReader br = new BufferedReader(isr);  
		    String line = null;  
		    System.out.println("<ERROR>");  
		    while ((line = br.readLine()) != null)  
		        System.out.println(line);  
		    System.out.println("</ERROR>");  
		    long exitVal = proc.waitFor();  
		    System.out.println("Process exitValue: " + exitVal);  
		} catch (Throwable t) {  
		    t.printStackTrace();  
		}  
	}
	
	public static void eeee(){
		File[] roots = File.listRoots();
		for (File drive : roots) {
		   System.out.println(drive.getPath());
		  }
		 System.out.println("***MAC地址***");  
	        //****************获取硬盘ID*****************//  
	        String sn = DiskUtils.getSerialNumber("C");   
	        System.out.println("***硬盘编号***");  
	        System.out.println(sn);   
	}
	
	public static void main(String[] args) {
		//String commandStr = "ping www.taobao.com";
		String commandStr = "diskpart ; list disk";
		//Command.exeCmd(commandStr);
		//bbbb();
//		cccc();
//		dddd();
		eeee();
	}
}
</pre><pre name="code" class="java">
package com.utils;


import java.io.File;


import javax.swing.filechooser.FileSystemView;


/**
 * 磁盘检查
 * 1、检测磁盘是本地磁盘还是可移动磁盘;
 * 2、显示相应的磁盘名称,磁盘总空间以及可用的空间
 */
public class DiskTest {
<span style="white-space:pre">	</span>private final static String localDiskName = "本地磁盘";
<span style="white-space:pre">	</span>private final static String removableDiskName = "可移动磁盘";
<span style="white-space:pre">	</span>private final static String enLocalDiskName = "Local Disk";
<span style="white-space:pre">	</span>private final static String enRemovableDiskName = "Removable Disk";


<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 获取磁盘信息
<span style="white-space:pre">	</span> * @return Map<String,ReturnPo>
<span style="white-space:pre">	</span> */


<span style="white-space:pre">	</span>public static void getDiskInfo() {
<span style="white-space:pre">		</span>FileSystemView fileSystemView = FileSystemView.getFileSystemView();// 获取FileSystemView对象
<span style="white-space:pre">		</span>File[] roots = File.listRoots();
<span style="white-space:pre">		</span>for (File file : roots) {
<span style="white-space:pre">			</span>String pathName = file.getPath();// 获取磁盘的路径名称
<span style="white-space:pre">			</span>String diskName = fileSystemView.getSystemTypeDescription(file);// 获取磁盘的类型描述信息
<span style="white-space:pre">			</span>if (diskName.startsWith(localDiskName)
<span style="white-space:pre">			</span>|| diskName.startsWith(enLocalDiskName))// 当磁盘为本地磁盘时
<span style="white-space:pre">			</span>{
<span style="white-space:pre">			</span>} else if (diskName.startsWith(removableDiskName)
<span style="white-space:pre">			</span>|| diskName.startsWith(enRemovableDiskName))// 当磁盘为可移动磁盘时
<span style="white-space:pre">			</span>{
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>System.out.println("磁盘名称为:" + diskName + pathName);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * @param args
<span style="white-space:pre">	</span> */


<span style="white-space:pre">	</span>public static void main(String[] args) {
<span style="white-space:pre">		</span>DiskTest.getDiskInfo();
<span style="white-space:pre">	</span>}
}
</pre><pre name="code" class="java">
</pre><pre name="code" class="java">package com.utils;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
  
class DiskUtils {  
    private DiskUtils() {  
    }  
  
    public static String getSerialNumber(String drive) {  
        String result = "";  
        try {  
            File file = File.createTempFile("d00000000", ".txt");
            FileWriter fw = new java.io.FileWriter(file);
            String vbs = "list disk"; // see note
            fw.write(vbs);
            fw.close();
            Process p = Runtime.getRuntime().exec("diskpart /s " + file.getPath());
            System.out.println(file.getPath());
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream(),"GBK"));  
            String line;
            while ((line = input.readLine()) != null) {
            <span style="white-space:pre">	</span>System.out.println(line);
            }  
            input.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return result.trim();  
    }  
}  

版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)