zoukankan      html  css  js  c++  java
  • Java中windows路径转换成linux路径等工具类

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

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

      1 import java.net.InetAddress;
      2 import java.net.UnknownHostException;
      3 import java.util.List;
      4 
      5 /**
      6  * OS Utility Class This is used to obtain the os related var programmatically
      7  * 
      8  * <p>
      9  * <a h ref="OSUtil.java.html"><i>View Source</i></a>
     10  * </p>
     11  * 
     12  *  13  */
     14 public class OSUtil {
     15     
     16     public static String LIUNX = "Linux";
     17     public static String WINDOWS = "Windows";
     18     
     19     
     20     /**
     21      * 功能: 将windows路径转换成linux路径
     22      */
     23     public static String 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 = new SysstoredevMgr() ;
     31             List<Sysstoredev> _list = _sM.findAll() ;
     32             for( Sysstoredev _sd : _list ){
     33                 String _driver = ConvertString.ConvertStr(_sd.getDriver()) ;
     34                 if(_path.startsWith(_driver)){
     35                      return FileNameUtil.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 string
     46      * 
     47      * @return String
     48      * @throws UnknownHostException
     49      */
     50     public static String getHostName() throws UnknownHostException {
     51         InetAddress inetaddr = InetAddress.getLocalHost();
     52         return inetaddr.getHostName();
     53     }
     54 
     55     /**
     56      * 获得主机IP
     57      * obtain the ip address of the host in form of string
     58      * 
     59      * @return String
     60      * @throws UnknownHostException
     61      */
     62     public static String getHostIP() throws UnknownHostException {
     63         InetAddress inetaddr = InetAddress.getLocalHost();
     64         return inetaddr.getHostAddress();
     65     }
     66 
     67     /**
     68      * 测试给定的主机是否是本地主机.
     69      * check if the given host is the local host
     70      * 
     71      * @param hostname String
     72      * @param hostip   String
     73      * @return boolean
     74      * @throws UnknownHostException
     75      */
     76     public static boolean isNative(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                 return inetaddr.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                 return b;
     98             } else {
     99                 return true;
    100             }
    101         } catch (UnknownHostException e) {
    102             return false;
    103         }
    104     }
    105 
    106     /**
    107      * 获得指定的环境变量的值
    108      * @param envvarname
    109      * @param defaultvalue
    110      * @return
    111      */
    112     public static String 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 static Boolean 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) throws Exception{
    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 }
  • 相关阅读:
    复制表部分内容到另一个表
    delphi使用ODAC控件TOraQuery操作表数据
    delphi使用ODAC控件查询存储过程返回结果集
    delphi使用ODAC控件事务处理(缓存提交)
    delphi使用ODAC控件事务处理(自动提交)
    delphi使用ODAC控件异步读取数据
    delphi使用ODAC控件常用功能
    delphi使用ODAC控件操作本地数据集
    时间限制关闭窗体的几点体会
    一个简单的双缓冲队列
  • 原文地址:https://www.cnblogs.com/DreamDrive/p/5760140.html
Copyright © 2011-2022 走看看