package common; import java.io.*; import java.net.*; import java.util.*; public class Common { @SuppressWarnings("rawtypes") public static String getClassLocation(Class cls) { URL url = cls.getProtectionDomain().getCodeSource().getLocation(); String filePath = null; try { filePath = URLDecoder.decode(url.getPath(), "UTF-8"); } catch (Exception e) { throw new RuntimeException(e); } if (filePath.endsWith(".jar")) { filePath = filePath.substring(0, filePath.lastIndexOf("/") + 1); } return new File(filePath).getAbsolutePath(); } @SuppressWarnings("rawtypes") public static String getWorkDir(Class cls) { String bin = "bin"; String workDir = getClassLocation(cls); if (workDir.endsWith(bin)) { workDir = workDir.substring(0, workDir.length() - bin.length()); workDir = new File(workDir).getAbsolutePath(); } return workDir; } public static String getLineSeparator() { return System.getProperty("line.separator"); } public static String getCurDir() { return System.getProperty("user.dir"); } public static Properties loadProperties(String filename) { Properties properties = new OrderedProperties(); try { FileInputStream input = new FileInputStream(filename); properties.load(input); input.close(); } catch (Exception e) { e.printStackTrace(); } return properties; } public static void saveProperties(Properties properties, String filename, String comments) throws IOException { try { File file = new File(filename); file.createNewFile(); FileOutputStream output = new FileOutputStream(file); properties.store(output, comments); output.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } public static void main(String[] args) { System.out.println(getWorkDir(Common.class)); } }