小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

java獲取當(dāng)前路徑(包含tomcat)

 燮羽 2010-12-15

1、利用System.getProperty()函數(shù)獲取當(dāng)前路徑:
System.out.println(System.getProperty("user.dir"));//user.dir指定了當(dāng)前的路徑

2、使用File提供的函數(shù)獲取當(dāng)前路徑:
File directory = new File("");//設(shè)定為當(dāng)前文件夾
try{
    System.out.println(directory.getCanonicalPath());//獲取標(biāo)準(zhǔn)的路徑
    System.out.println(directory.getAbsolutePath());//獲取絕對(duì)路徑
}catch(Exceptin e){}

File.getCanonicalPath()和File.getAbsolutePath()大約只是對(duì)于new File(".")和new File("..")兩種路徑有所區(qū)別。

# 對(duì)于getCanonicalPath()函數(shù),“."就表示當(dāng)前的文件夾,而”..“則表示當(dāng)前文件夾的上一級(jí)文件夾
# 對(duì)于getAbsolutePath()函數(shù),則不管”.”、“..”,返回當(dāng)前的路徑加上你在new File()時(shí)設(shè)定的路徑
# 至于getPath()函數(shù),得到的只是你在new File()時(shí)設(shè)定的路徑

比如當(dāng)前的路徑為 C:\test :
File directory = new File("abc");
directory.getCanonicalPath(); //得到的是C:\test\abc
directory.getAbsolutePath();    //得到的是C:\test\abc
direcotry.getPath();                    //得到的是abc

File directory = new File(".");
directory.getCanonicalPath(); //得到的是C:\test
directory.getAbsolutePath();    //得到的是C:\test\.
direcotry.getPath();                    //得到的是.

File directory = new File("..");
directory.getCanonicalPath(); //得到的是C:\
directory.getAbsolutePath();    //得到的是C:\test\..
direcotry.getPath();                    //得到的是..

 


另外:System.getProperty()中的字符串參數(shù)如下:

System.getProperty()參數(shù)大全
# java.version                                Java Runtime Environment version 
# java.vendor                                Java Runtime Environment vendor 
# java.vendor.url                           Java vendor URL 
# java.home                                Java installation directory 
# java.vm.specification.version   Java Virtual Machine specification version 
# java.vm.specification.vendor    Java Virtual Machine specification vendor 
# java.vm.specification.name      Java Virtual Machine specification name 
# java.vm.version                        Java Virtual Machine implementation version 
# java.vm.vendor                        Java Virtual Machine implementation vendor 
# java.vm.name                        Java Virtual Machine implementation name 
# java.specification.version        Java Runtime Environment specification version 
# java.specification.vendor         Java Runtime Environment specification vendor 
# java.specification.name           Java Runtime Environment specification name 
# java.class.version                    Java class format version number 
# java.class.path                      Java class path 
# java.library.path                 List of paths to search when loading libraries 
# java.io.tmpdir                       Default temp file path 
# java.compiler                       Name of JIT compiler to use 
# java.ext.dirs                       Path of extension directory or directories 
# os.name                              Operating system name 
# os.arch                                  Operating system architecture 
# os.version                       Operating system version 
# file.separator                         File separator ("/" on UNIX) 
# path.separator                  Path separator (":" on UNIX) 
# line.separator                       Line separator ("\n" on UNIX) 
# user.name                        User's account name 
# user.home                              User's home directory 
# user.dir                               User's current working directory

tomcat下獲得項(xiàng)目的絕對(duì)路徑

private String projectName="sz_pro";        //  你項(xiàng)目的名稱

//獲取當(dāng)前項(xiàng)目的絕對(duì)路徑
  public String getPorjectPath(){
   String nowpath;             //當(dāng)前tomcat的bin目錄的路徑 如 D:\java\software\apache-tomcat-6.0.14\bin
   String tempdir;
   nowpath=System.getProperty("user.dir");
   tempdir=nowpath.replace("bin", "webapps");  //把bin 文件夾變到 webapps文件里面 
   tempdir+="\\"+projectName;  //拼成D:\java\software\apache-tomcat-6.0.14\webapps\sz_pro 
   return tempdir;  
  }
 
另外還有一個(gè)方法可以用來獲取類的絕對(duì)路徑(當(dāng)前的classpath)
getClass().getResource("/").toString();

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多