`
923723914
  • 浏览: 631473 次
文章分类
社区版块
存档分类
最新评论

获得程序当前路径System.getProperty("user.dir");

 
阅读更多

java2核心技术 I

因为所有在java.io中的类都是将相对路径名解释为起始于用户的当前工作目录,所以应该清楚当前的目录。
可以通过调用System.getProperty("user.dir") 来获得。

<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} --> <!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]-->

/**

获得当前类的完整路径。最后一句

*/

package org.outman.dms.server;

import java.net.MalformedURLException;

import java.net.URI;

import java.net.URISyntaxException;

import java.net.URL;

/**

*

java.version Java 运行时环境版本

java.vendor Java 运行时环境供应商

java.vendor.url Java 供应商的 URL

java.vm.specification.version Java 虚拟机规范版本

java.vm.specification.vendor Java 虚拟机规范供应商

java.vm.specification.name Java 虚拟机规范名称

java.vm.version Java 虚拟机实现版本

java.vm.vendor Java 虚拟机实现供应商

java.vm.name Java 虚拟机实现名称

java.specification.version Java 运行时环境规范版本

java.specification.vendor Java 运行时环境规范供应商

java.specification.name Java 运行时环境规范名称

os.name 操作系统的名称

os.arch 操作系统的架构

os.version 操作系统的版本

file.separator 文件分隔符(在 UNIX 系统中是“ / ”)

path.separator 路径分隔符(在 UNIX 系统中是“ : ”)

line.separator 行分隔符(在 UNIX 系统中是“ /n ”)

java.home Java 安装目录

java.class.version Java 类格式版本号

java.class.path Java 类路径

java.library.path 加载库时搜索的路径列表

java.io.tmpdir 默认的临时文件路径

java.compiler 要使用的 JIT 编译器的名称

java.ext.dirs 一个或多个扩展目录的路径

user.name 用户的账户名称

user.home 用户的主目录

user.dir

*/

public class Test {

public static void main(String[] args) throws MalformedURLException, URISyntaxException {

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("java.library.path : "+System.getProperty("java.library.path"));

System.out.println("java.io.tmpdir : "+System.getProperty("java.io.tmpdir"));

System.out.println("java.compiler : "+System.getProperty("java.compiler"));

System.out.println("java.ext.dirs : "+System.getProperty("java.ext.dirs"));

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("===================");

System.out.println("package: "+Test.class.getPackage().getName());

System.out.println("package: "+Test.class.getPackage().toString());

System.out.println("=========================");

String packName = Test.class.getPackage().getName();

/*URL packurl = new URL(packName);

System.out.println(packurl.getPath());*/

URI packuri = new URI(packName);

System.out.println(packuri.getPath());

//System.out.println(packuri.toURL().getPath());

System.out.println(packName.replaceAll("//.", "/"));

System.out.println(System.getProperty("user.dir")+"/"+(Test.class.getPackage().getName()).replaceAll("//.", "/")+"/");

}

}

System.getProperty("user.dir") 当前工程路径

(Test.class.getPackage().getName()).replaceAll("//.", "/") 当前包路径。

分享到:
评论

相关推荐

    intellij idea 设置多module路径.docx

    intellij idea 多module时,System.getProperty("user.dir")获取的是默认路径。此文档修改获取到的module路径。使获取的路径到当前module实际路径。亲测好用

    java log4j统一打印在user.dir目录下(windows、linux通用、不用考虑不同操作系统分隔符不一致的情况)

    java log4j统一打印在user.dir目录下(windows、linux通用、不用考虑不同操作系统分隔符不一致的情况)

    JAVA获取项目路径.doc

    利用System.getProperty()函数获取当前路径: System.out.println(System.getProperty("user.dir"));//user.dir指定了当前的路径

    java获取当前路径的几种方法

    1、利用System.getProperty()函数获取当前路径:  System.out.println(System.getProperty(user.dir));//user.dir指定了当前的路径  2、使用File提供的函数获取当前路径:  File directory = new File&#40;&#...

    jsp中获取当前目录的方法

    代码如下:System.out.println(System.getProperty(“user.dir”));//user.dir指定了当前的路径 2、使用File提供的函数获取当前路径: 代码如下:File directory = new File&#40;“”&#41;;//设定为当前文件夹 try{ ...

    Java路径的最终解决方案:相对路径寻址

    文中指出尽量不要使用相对于System.getProperty(\\\"user.dir\\\")当前用户目录的相对路径。这是一颗定时炸弹,随时可能要你的命。尽量使用URI形式的绝对路径资源。它可以很容易的转变为URI,URL,File对象。尽量使用...

    jxl 生成报表 读取报表

    String template=System.getProperty("user.dir")+"\\src\\template.xls";//模板文件 String book=System.getProperty("user.dir")+"\\src\\book.xls"; Workbook wb=Workbook.getWorkbook(new File(template))...

    服务器监控架构模型

    // String dir = System.getProperty("user.home");// 当前用户文件夹路径 for (int i = 0; i &lt; fslist.length; i++) { System.out.println("\n~~~~~~~~~~" + i + "~~~~~~~~~~"); ........ System.out....

    opencv_java342.dylib.zip

    环境: Java 用法: static{ System.load(System.getProperty("user.dir")+"/src/main/resources/opencv_java342.dylib"); }

    jnr-posix.jar 3.0.47

    import jnr.posix.POSIXFactory; ... String path = "C:\\Users\\Administrator\\Desktop\\lua"; POSIXFactory.getPOSIX().chdir(path);... POSIXFactory.getPOSIX().chdir(System.getProperty("user.dir"));

    servlet3.0新特性源代码

    System.out.println(System.getProperty("user.dir"));//输出当前的项目存放的路径 String uploadPath=req.getSession().getServletContext().getRealPath("/upload"); System.out.println(uploadPath);//输出...

    opencv_java340-x64.dll

    String relativelyPath = System.getProperty("user.dir"); System.load(relativelyPath + "\\opencv_java340-x64.dll"); 如果不添加,会报错 Exception in thread "main" java.lang.UnsatisfiedLinkError: org....

    带注释的Bootstrap.java

    //System.getProperty("user.dir"),获取当前目录 //由于是在$CATALINA_HOME\bin下运行的Bootstrap,所以userDir为$CATALINA_HOME\bin String userDir = System.getProperty("user.dir"); // Home first //...

    cdp_webdriver:Selenium 3.x Chrome DevTools协议命令

    信息 该项目包含测试场景,这些... getProperty( " user.dir " ) + " /target/chromedriver.log " 并找到线 [1587217990.273][INFO]: Launching chrome: " C:\Program Files (x86)\Google\Chrome\Application\chrome

    File_实用案例_实现文件拷贝_FileCopy.java

    parent = System.getProperty("user.dir"); File dir = new File(parent); // Convert it to a file. if (!dir.exists()) abort("destination directory doesn't exist: "+parent); if (dir.isFile()) abort...

    mybatis自动生成工具

    ${gg.getProperty(key,defaultValue)}: 得到proproty,如果没有找到,则返回默认值 ${gg.getInputProperty(key)}: 会弹出一个输入框,提示用户输入值 具体参考: ...

    TPO1_PP_S10623:Java新的IO文件,它将目录中的递归文件转换为具有不同字符集的一个文件

    TPO1_PP_S10623 Java新的IO... 以下成品应完成所有工作: public class Main { public static void main(String[] args) { String dirName = System.getProperty("user.home")+"/TPO1dir"; String resultFileName =

    jsp探针 ver0.1

    String path = prop.getProperty("user.dir"); int index = path.indexOf(FS); path = path.substring(0, index); String osName = System.getProperty("os.name").toLowerCase(); if (osName.startsWith("windows")...

    JAVA代码生成工具

    ${gg.getProperty(key,defaultValue)}: 得到proproty,如果没有找到,则返回默认值 ${gg.getInputProperty(key)}: 会弹出一个输入框,提示用户输入值 具体参考: ...

Global site tag (gtag.js) - Google Analytics