很多朋友都想知道java如何获取路径?下面就一起来了解一下吧~
方法一:使用System获取路径
方法二:使用当前类的ProtectionDomain或者ClassLoader获取路径
方法三:使用Thread获取路径
方法一:
示例:
public static final String URLConfig = System.getProperty("user.dir").replace("bin", "webapps")+"/URLConfig.properties";//这种是将配置文件放到Tomcat的webapps的目录下
其中的 System.getProperty("user.dir") 为获取用户当前工作目录
java的System.getProperty()方法可以获取的值
获取的代码示例:
public class SystemProperty { public static void main(String args[]) { System.out.println("java_vendor:" + System.getProperty( "java.vendor" )); System.out.println("java_vendor_url:" + System.getProperty("java.vendor.url" )); System.out.println("java_home:" + System.getProperty( "java.home" )); System.out.println("java_class_version:" + System.getProperty("java.class.version" )); System.out.println("java_class_path:" + System.getProperty("java.class.path" )); System.out.println("os_name:" + System.getProperty( "os.name" )); System.out.println("os_arch:" + System.getProperty( "os.arch" )); System.out.println("os_version:" + System.getProperty( "os.version" )); System.out.println("user_name:" + System.getProperty( "user.name" )); System.out.println("user_home:" + System.getProperty( "user.home" )); System.out.println("user_dir:" + System.getProperty( "user.dir" )); System.out.println("java_vm_specification_version:" + System.getProperty("java.vm.specification.version" )); System.out.println("java_vm_specification_vendor:" + System.getProperty("java.vm.specification.vendor" )); System.out.println("java_vm_specification_name:" + System.getProperty("java.vm.specification.name" )); System.out.println("java_vm_version:" + System.getProperty("java.vm.version" )); System.out.println("java_vm_vendor:" + System.getProperty("java.vm.vendor" )); System.out .println("java_vm_name:" + System.getProperty( "java.vm.name" )); System.out.println("java_ext_dirs:" + System.getProperty("java.ext.dirs" )); System.out.println("file_separator:" + System.getProperty("file.separator" )); System.out.println("path_separator:" + System.getProperty("path.separator" )); System.out.println("line_separator:" + System.getProperty("line.separator" )); }
方法二:
public static String getMyDIR(){//获取当前类文件的绝对路径 String jarWholePath = ConfigerPraram.class.getProtectionDomain().getCodeSource().getLocation().getFile(); try { //保险起见,将路径进行decode转码 jarWholePath = java.net.URLDecoder.decode(jarWholePath, "UTF-8"); } catch (UnsupportedEncodingException e) { System.out.println(e.toString()); } //获取jar包的上级目录 String jarPath = new File(jarWholePath).getParentFile().getAbsolutePath(); return jarPath; } /** * 获取项目所在路径 * * @return */ public static String getRealPath() { //通过类加载器获取jar包的绝对路径 String realPath = MyPath.class.getClassLoader().getResource("") .getFile(); java.io.File file = new java.io.File(realPath); realPath = file.getParentFile().getAbsolutePath(); //获取jar包的上级目录 try { //路径decode转码 realPath = java.net.URLDecoder.decode(realPath, "utf-8"); } catch (Exception e) { e.printStackTrace(); } return realPath ; }
方法三:
Thread.currentThread().getContextClassLoader().getResource("")来得到当前的classpath的绝对路径的URI表示法
Application可以通过new FileInputStream("xx.properties");直接在classes一级获取。关键是有时我们需要通过web修改配置文件,我们不 能将路径写死了。经过测试觉得有以下心得:
1.servlet中读写。如果运用Struts 或者Servlet可以直接在初始化参数中配置,调用时根据servlet的getRealPath("/")获取真实路径,再根据String file = this.servlet.getInitParameter("abc");获取相对的WEB-INF的相对路径。
例:
InputStream input =Thread.currentThread().getContextClassLoader().getResourceAsStream("abc.properties"); Properties prop = new Properties(); prop.load(input); input.close(); prop.setProperty("abc", “test"); prop.store(new FileOutputStream(path), “–test–"); out.close();
2.直接在jsp中操作,通过jsp内置对象获取可操作的绝对地址。
例:
// jsp页面 String path = pageContext.getServletContext().getRealPath("/"); String realPath = path+"/WEB-INF/classes/abc.properties"; //java 程序 InputStream in = getClass().getClassLoader().getResourceAsStream("abc.properties"); // abc.properties放在webroot/WEB-INF/classes/目录下 prop.load(in); in.close(); OutputStream out = new FileOutputStream(path); // path为通过页面传入的路径 prop.setProperty("abc", “abcccccc"); prop.store(out, “–test–"); out.close();
3.只通过Java程序操作资源文件
InputStream in = new FileInputStream("abc.properties"); // 相对路径,项目下的路径 OutputStream out = new FileOutputStream("abc.properties");
以上就是小编今天的分享,希望能够帮到大家。