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

我总结的类似相关博客: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")); } }

最新文章

  1. javascript闭包
  2. C#使用Log4Net记录日志
  3. 炫酷的时钟--canvas初体验
  4. 跨进程(同一app不同进程之间通信)——Android自动化测试学习历程
  5. Java Web项目里开发获取上个页面连接地址的问题
  6. OC开发中运用到的枚举
  7. Linux简单程序实例(GNU工具链,进程,线程,无名管道pipe,基于fd的文件操作,信号,scoket)
  8. [CSS]列表属性(List)
  9. 1.1C++入门 未完待续。。。
  10. HTML5比较实用的代码
  11. Log4net 列
  12. Spring boot+CXF开发WebService
  13. Mongo汇总问题
  14. akoj-1369 贪吃蛇
  15. Painter&#39;s Problem poj1681 高斯消元法
  16. Fiddler的script用法
  17. 图书管理系统(无中间件,用装饰器的)-----未基于FORM组件
  18. Python3实战系列之九(获取印度售后数据项目)
  19. [EXP]Apache Superset &lt; 0.23 - Remote Code Execution
  20. 模拟一个带背景的 TPanel

热门文章

  1. linux下开发c第一弹--相关环境需求
  2. HTML5每日一练之input新增加的URL类型与email类型应用
  3. centos5 vim升级到7.4
  4. &lt;math.h&gt;与&lt;float.h&gt;
  5. Windows下sqlmap的使用_01
  6. Thinkphp框架 -- ajax无刷新上传图片
  7. [置顶] stax解析xml文档的6种方式
  8. Codeforces Gym 100286B Blind Walk DFS
  9. EDIT Ini写Ini配置
  10. 三分钟掌握 JUnit3.0