java转linux_Java中windows路径转换成linux路径等工具类

1 importjava.net.InetAddress;2 importjava.net.UnknownHostException;3 importjava.util.List;4

5 /**

6 * OS Utility Class This is used to obtain the os related var programmatically7 *8 *

9 * View Source10 *

11 *12 *13 */

14 public classOSUtil {15

16 public static String LIUNX = "Linux";17 public static String WINDOWS = "Windows";18

19

20 /**

21 * 功能: 将windows路径转换成linux路径22 */

23 public staticString convert2linuxPath(String _path){24 if(isLinuxSystem()){25 int index = _path.indexOf(":");26 if(index>1 || index == -1) //不含有盘符

27

28 return_path;29

30 SysstoredevMgr _sM = newSysstoredevMgr() ;31 List _list =_sM.findAll() ;32 for( Sysstoredev _sd : _list ){33 String _driver =ConvertString.ConvertStr(_sd.getDriver()) ;34 if(_path.startsWith(_driver)){35 returnFileNameUtil.correctFileName4Linux(_path.replace(_driver, ConvertString.ConvertStr(_sd.getLpath()))) ;36 }37 }38 }39 return_path;40 }41

42

43 /**

44 * 获得主机名称45 * obtain the host name in form of string46 *47 *@returnString48 *@throwsUnknownHostException49 */

50 public static String getHostName() throwsUnknownHostException {51 InetAddress inetaddr =InetAddress.getLocalHost();52 returninetaddr.getHostName();53 }54

55 /**

56 * 获得主机IP57 * obtain the ip address of the host in form of string58 *59 *@returnString60 *@throwsUnknownHostException61 */

62 public static String getHostIP() throwsUnknownHostException {63 InetAddress inetaddr =InetAddress.getLocalHost();64 returninetaddr.getHostAddress();65 }66

67 /**

68 * 测试给定的主机是否是本地主机.69 * check if the given host is the local host70 *71 *@paramhostname String72 *@paramhostip String73 *@returnboolean74 *@throwsUnknownHostException75 */

76 public static booleanisNative(String hostname, String hostip) {77 try{78 hostname = (hostname == null ? "": hostname);79 hostip = (hostip == null ? "": hostip);80

81 InetAddress inetaddr =InetAddress.getLocalHost();82 if (hostname.equals(""))83 returninetaddr.getHostAddress().equalsIgnoreCase(hostip);84

85 if (!inetaddr.getHostName().equalsIgnoreCase(hostname))86 return false;87

88 if (hostip.length() > 0) {89 InetAddress[] inetaddrs =InetAddress.getAllByName(inetaddr.getHostName());90 boolean b = false;91 for (int i = 0; i < inetaddrs.length; i++) {92 if(inetaddrs[i].getHostAddress().equalsIgnoreCase(hostip)) {93 b = true;94 break;95 }96 }97 returnb;98 } else{99 return true;100 }101 } catch(UnknownHostException e) {102 return false;103 }104 }105

106 /**

107 * 获得指定的环境变量的值108 *@paramenvvarname109 *@paramdefaultvalue110 *@return

111 */

112 public staticString getEnvironmentVar(String envvarname,113 String defaultvalue) {114 String str =System.getenv(envvarname);115 str = (str == null ? "": str);116 return (str.length() == 0 ?defaultvalue : str);117 }118

119

120 /**

121 * 判断是否是Linux操作系统122 *@return

123 */

124 public staticBoolean isLinuxSystem(){125 if(OSUtil.LIUNX.equals(System.getProperty("os.name"))){126 return true;127 }128 return false;129 }130

131

132 public static void main(String[] args) throwsException{133

134 System.out.println(OSUtil.convert2linuxPath("M:\\hello\\abc.txt"));135

136 System.out.println(OSUtil.convert2linuxPath("M:\\hello/abc.txt"));137

138 System.out.println(OSUtil.convert2linuxPath("/linux/p\\u.png"));139

140 }141

142 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值