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

我总结的类似相关博客: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. Java处理InterruptedException异常小结

    对于InterruptedException,一种常见的处理方式是捕捉它,然后什么也不做(或者记录下它,不过这也好不到哪去).不幸的是,这种方法忽略了这样一个事实:这期间可能发生中断,而中断可能导致应 ...

  2. Linux下文件的压缩与打包

    一.Linux下常见的文件压缩命令: 在Linux的环境中,压缩文件的扩展名大多是:『*.tar, *.tar.gz, *.tgz, *.gz, *.Z, *.bz2』,为什么会有这样的扩展名呢? 这 ...

  3. js特效-仿照html属性title写一个弹出标题样式

    问题场景:商品描述,当营业员给客户介绍时会看着这些弹出标题来给客户讲解描述,一般采用html中属性title来实现,但是有些商品描述太长,这些title在IE浏览器中大约展示5s,营业员需要多次移动鼠 ...

  4. HDU 3416 Marriage Match IV (求最短路的条数,最大流)

    Marriage Match IV 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q Description Do not si ...

  5. eclipse中的js文件报错的解决办法

    在使用别人的项目的时候,导入到eclipse中发现js文件报错,解决办法是关闭eclipse的js校验功能. 三个步骤: 1. 右键点击项目->properties->Validation ...

  6. 用鼠标右键选择DataGridView单元格或行

    在datagirdview_cellmousedown事件中先将CurrentCell(或CurrentRow)的Selected属性设为false,然后将鼠标右键点击的单元格(或行)设为Curren ...

  7. Win7关机出现关闭程序提示框

    运行输入Gpedit.msc回车打开组策略,在左侧选计算机配置/管理模板/系统/关机选项,在右侧双击“关闭会阻止或取消关机的应用程序的自动终止功能”,在打开的提示框中选“已启用”,按确定即可.

  8. 简单http笔记

    https是以安全为目的的网络传输协议,可以认为是http的安全版,https使用ssl协议保证安全传输.https位于网络模型的应用层,使用默认端口443进行通信,URL以https开头是https ...

  9. linux下为php添加mongodb扩展

    基于本博客yum安装的lamp环境,phpize 位于 /usr/bin,php-config 位于/usr/bin,php.ini 位于/etc/ 1.首先从http://pecl.php.net/ ...

  10. C#不错的扩展工具类

    FSLibExtension.NET https://github.com/iccfish/FSLib.Extension WebEssentials2013 https://github.com/i ...