miduo_server/gamecommon/src/main/java/util/SysUtil.java

69 lines
2.2 KiB
Java

package util;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
public class SysUtil {
public static String getLocalHostIp() throws Exception {
InetAddress addr = InetAddress.getLocalHost();
return addr.getHostAddress();
}
public static boolean isWindows() {
String osName = System.getProperty("os.name");
return osName.matches("^(?i)Windows.*$");
}
public static String getServerConfPath(String... filePath) throws IOException {
return getPath("serverconf", filePath).toString();
}
public static String getPath(String prefixDir, String... filePath) throws IOException {
StringBuilder path = new StringBuilder();
path.append(getRootPath()).append(prefixDir);
for (String p : filePath) {
path.append(File.separator).append(p);
}
return path.toString();
}
public static String getRootPath() throws IOException {
StringBuilder path = new StringBuilder();
if (SysUtil.isWindows()) {// Window 系统
path.append(new File(".").getCanonicalPath()).append(File.separator);
}else {
path.append("../");
}
return path.toString();
}
public static void main(String[] args) throws IOException {
System.out.println(getPath("fight\\src\\main\\resources\\hello.lua"));
System.out.println(getRootPath());
System.out.println(getPath("fight\\src\\com\\ljsd\\gameserver\\user\\dao\\"));
}
// public static String getConfPath(String... filePath) throws IOException {
// StringBuilder path = new StringBuilder();
// if (SysUtil.isWindows()) {// Window 系统
// StringBuilder sb = new StringBuilder();
// for (String p : filePath) {
// sb.append("\\").append(p);
// }
// path.append(new File(".").getCanonicalPath()).append("\\conf").append(sb);
// } else {// Linux 系统
// StringBuilder sb = new StringBuilder();
// for (String p : filePath) {
// sb.append("/").append(p);
// }
// path.append("../conf").append(sb);
// }
// return path.toString();
// }
}