Vincent

          Vicent's blog
          隨筆 - 74, 文章 - 0, 評(píng)論 - 5, 引用 - 0
          數(shù)據(jù)加載中……

          J2SE5.0新特性之ProcessBuilder

          這個(gè)例子使用了J2SE5.0的ProcessBuilder類執(zhí)行外部的程序,相對(duì)于?Runtime.exec?,它更方便,可以設(shè)置環(huán)境變量等。這里使用它在windows下讀取物理網(wǎng)卡的地址

          1. package?com.kuaff.jdk5package;

          2. import?java.io.IOException;
          3. import?java.io.InputStream;
          4. import?java.util.ArrayList;
          5. import?java.util.List;

          6. public?class?ProcessBuilderShow
          7. {
          8. ????public?static?List<String>?getPhysicalAddress()
          9. ????{
          10. ????????Process?p?=?null;
          11. ????????//物理網(wǎng)卡列表
          12. ????????List<String>?address?=?new?ArrayList<String>();

          13. ????????try
          14. ????????{
          15. ????????????//執(zhí)行ipconfig?/all命令
          16. ????????????p?=?new?ProcessBuilder("ipconfig",?"/all").start();
          17. ???????? }
          18. ????????catch?(IOException?e)
          19. ????????{
          20. ????????????return??address;
          21. ???????? }
          22. ????????byte[]?b?=?new?byte[1024];
          23. ????????StringBuffer?sb?=?new?StringBuffer();
          24. ????????//讀取進(jìn)程輸出值
          25. ????????InputStream?in?=?p.getInputStream();
          26. ????????try
          27. ????????{
          28. ????????????while?(in.read(b)>0)
          29. ????????????{
          30. ????????????????sb.append(new?String(b));
          31. ???????????? }
          32. ???????? }
          33. ????????catch?(IOException?e1)
          34. ????????{
          35. ???????? }
          36. ????????finally
          37. ????????{
          38. ????????????try
          39. ????????????{
          40. ????????????????in.close();
          41. ???????????? }
          42. ????????????catch?(IOException?e2)
          43. ????????????{
          44. ???????????? }
          45. ???????? }
          46. ????????//以下分析輸出值,得到物理網(wǎng)卡
          47. ????????String?rtValue?=?sb.substring(0);
          48. ????????int?i?=?rtValue.indexOf("Physical?Address.?.?.?.?.?.?.?.?.?:");
          49. ????????while(i>0)
          50. ????????{
          51. ????????????rtValue?=?rtValue.substring(i?+?"Physical?Address.?.?.?.?.?.?.?.?.?:".length());
          52. ????????????address.add(rtValue.substring(0,18));
          53. ????????????i?=?rtValue.indexOf("Physical?Address.?.?.?.?.?.?.?.?.?:");
          54. ???????? }
          55. ????????
          56. ????????return?address;
          57. ????????
          58. ???? }
          59. ????public?static?void?main(String[]?args)
          60. ????{
          61. ????????List<String>?address?=?ProcessBuilderShow.getPhysicalAddress();
          62. ????????for(String?add:address)
          63. ????????{
          64. ????????????System.out.printf("物理網(wǎng)卡地址:%s%n",?add);
          65. ???????? }
          66. ???? }
          67. }

          posted on 2006-08-22 10:45 Binary 閱讀(125) 評(píng)論(0)  編輯  收藏 所屬分類: j2se

          主站蜘蛛池模板: 天等县| 宜君县| 庆阳市| 区。| 桃江县| 定陶县| 诏安县| 义马市| 临颍县| 宜宾县| 沙坪坝区| 东源县| 安龙县| 广汉市| 小金县| 临沧市| 孟州市| 隆林| 延长县| 江西省| 汉源县| 六盘水市| 林口县| 吉木萨尔县| 崇明县| 绥中县| 镇康县| 中阳县| 赣州市| 天峻县| 河津市| 赣榆县| 平邑县| 德江县| 西畴县| 合水县| 瓮安县| 卓尼县| 双桥区| 永州市| 田阳县|