项目中发现别人写好的操作系统相关的工具类:

我总结的类似相关博客:http://www.cnblogs.com/DreamDrive/p/4289860.html

 import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List; /**
* OS Utility Class This is used to obtain the os related var programmatically
*
* <p>
* <a h ref="OSUtil.java.html"><i>View Source</i></a>
* </p>
*
* */
public class OSUtil { public static String LIUNX = "Linux";
public static String WINDOWS = "Windows"; /**
* 功能: 将windows路径转换成linux路径
*/
public static String convert2linuxPath(String _path){
if(isLinuxSystem()){
int index = _path.indexOf(":");
if(index>1 || index == -1) //不含有盘符 return _path; SysstoredevMgr _sM = new SysstoredevMgr() ;
List<Sysstoredev> _list = _sM.findAll() ;
for( Sysstoredev _sd : _list ){
String _driver = ConvertString.ConvertStr(_sd.getDriver()) ;
if(_path.startsWith(_driver)){
return FileNameUtil.correctFileName4Linux(_path.replace(_driver, ConvertString.ConvertStr(_sd.getLpath()))) ;
}
}
}
return _path;
} /**
* 获得主机名称
* obtain the host name in form of string
*
* @return String
* @throws UnknownHostException
*/
public static String getHostName() throws UnknownHostException {
InetAddress inetaddr = InetAddress.getLocalHost();
return inetaddr.getHostName();
} /**
* 获得主机IP
* obtain the ip address of the host in form of string
*
* @return String
* @throws UnknownHostException
*/
public static String getHostIP() throws UnknownHostException {
InetAddress inetaddr = InetAddress.getLocalHost();
return inetaddr.getHostAddress();
} /**
* 测试给定的主机是否是本地主机.
* check if the given host is the local host
*
* @param hostname String
* @param hostip String
* @return boolean
* @throws UnknownHostException
*/
public static boolean isNative(String hostname, String hostip) {
try {
hostname = (hostname == null ? "" : hostname);
hostip = (hostip == null ? "" : hostip); InetAddress inetaddr = InetAddress.getLocalHost();
if (hostname.equals(""))
return inetaddr.getHostAddress().equalsIgnoreCase(hostip); if (!inetaddr.getHostName().equalsIgnoreCase(hostname))
return false; if (hostip.length() > 0) {
InetAddress[] inetaddrs = InetAddress.getAllByName(inetaddr.getHostName());
boolean b = false;
for (int i = 0; i < inetaddrs.length; i++) {
if (inetaddrs[i].getHostAddress().equalsIgnoreCase(hostip)) {
b = true;
break;
}
}
return b;
} else {
return true;
}
} catch (UnknownHostException e) {
return false;
}
} /**
* 获得指定的环境变量的值
* @param envvarname
* @param defaultvalue
* @return
*/
public static String getEnvironmentVar(String envvarname,
String defaultvalue) {
String str = System.getenv(envvarname);
str = (str == null ? "" : str);
return (str.length() == 0 ? defaultvalue : str);
} /**
* 判断是否是Linux操作系统
* @return
*/
public static Boolean isLinuxSystem(){
if(OSUtil.LIUNX.equals(System.getProperty("os.name"))){
return true;
}
return false;
} public static void main(String[] args) throws Exception{ System.out.println(OSUtil.convert2linuxPath("M:\\hello\\abc.txt")); System.out.println(OSUtil.convert2linuxPath("M:\\hello/abc.txt")); System.out.println(OSUtil.convert2linuxPath("/linux/p\\u.png")); } }

Java中windows路径转换成linux路径等工具类的更多相关文章

  1. java中汉字自动转换成拼音

    java中汉字自动转换成拼音 1.需要下载jar包 pinyin4j.2.5.0.jar ,加入到WEB-INF下的lib里边,右键add to bulid path. 2.[代码]PinYinUti ...

  2. Java中创建操作文件和文件夹的工具类

    Java中创建操作文件和文件夹的工具类 FileUtils.java import java.io.BufferedInputStream; import java.io.BufferedOutput ...

  3. Java中的List转换成JSON报错(五)

    1.错误描述 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/beanu ...

  4. Java中的List转换成JSON报错(四)

    1.错误描述 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/colle ...

  5. Java中的List转换成JSON报错(二)

    1.错误描述 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/loggi ...

  6. Java中的List转换成JSON报错(一)

    1.错误描述 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/ ...

  7. Java中常用的加密方式(附多个工具类)

    一.Java常用加密方式 Base64加密算法(编码方式) MD5加密(消息摘要算法,验证信息完整性) 对称加密算法 非对称加密算法 数字签名算法 数字证书 二.分类按加密算法是否需要key被分为两类 ...

  8. JAVA中把ResultSet转换成LIST

    项目中老是遇到数据库异常关闭的情况,真烦, 想用hibernate呢,那个玩意儿又太笨重,感慨C#和PHP的舒适方便性,模拟TP写了个数据处理层,将就用着先代码里有很多项目中的东西,不要直接COPY了 ...

  9. Java中的List转换成JSON报错(三)

    1.错误描述 Exception in thread "main" java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher a ...

随机推荐

  1. HDU 5781 ATM Mechine (概率DP)

    ATM Mechine 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5781 Description Alice is going to take ...

  2. Spring EL Lists, Maps example

    In this article, we show you how to use Spring EL to get value from Map and List. Actually, the way ...

  3. Codeforces Round #271 (Div. 2) D. Flowers (递推)

    题目链接:http://codeforces.com/problemset/problem/474/D 用RW组成字符串,要求w的个数要k个连续出现,R任意,问字符串长度为[a, b]时,字符串的种类 ...

  4. IP访问SQL数据库设置

    http://wenku.baidu.com/link?url=mnjuPMo9qJvzluCHEvqVDawpuloKeGla05a2L3UtqzD_bF1VJMb7jHY4SBhuYH3-K_xF ...

  5. Spring Data JPA教程, 第四部分: JPA Criteria Queries(未翻译)

    The third part of my Spring Data JPA tutorialdescribed how you can create custom queries by using qu ...

  6. qemu 模拟-arm-mini2440开发板-启动u-boot,kernel和nfs文件系统

    qemu 本文介绍了如何编译u-boot.linux kernel,然后用qemu启动u-boot和linux kernel,达到与开发板上一样的学习效果! 虽然已经买了2440开发板,但是在实际学习 ...

  7. Jquery EasyUi实战教程布局篇

    转自:http://www.kwstu.com/ArticleView/kwstu_20139413501290 送给大家一个非常好的后台布局模板,本人后来就选择了这个模板http://www.kws ...

  8. 教你50招提升ASP.NET性能(二十二):利用.NET 4.5异步结构

    (40)Take advantage of .NET 4.5 async constructs 招数40: 利用.NET 4.5异步结构 With the arrival of .NET 4.5, w ...

  9. 在Android Eclipse 开发如何 使用 (*.aar)文件

    http://www.cnblogs.com/shortboy/p/4424944.html 开场白:其实这篇文章有点白费心机. 详细说明是:http://blog.csdn.net/qiujuer/ ...

  10. Actions 动作集

    --> 移动鼠标到指定位置(先触发onMouseOver动作)        Actions action = new Actions(driver);        WebElement th ...