1、利用System.getProperty()函數獲取當前路徑:
System.out.println(System.getProperty("user.dir"));//user.dir指定了當前的路徑
2、使用File提供的函數獲取當前路徑:
File directory = new File("");//設定為當前文件夾
try{
System.out.println(directory.getCanonicalPath());//獲取標準的路徑
System.out.println(directory.getAbsolutePath());//獲取絕對路徑
}catch(Exceptin e){}
File.getCanonicalPath()和File.getAbsolutePath()大約只是對於new File(".")和new
File("..")兩種路徑有所區別。
# 對於getCanonicalPath()函數,「."就表示當前的文件夾,而」..「則表示當前文件夾的上一級文件夾
# 對於getAbsolutePath()函數,則不管」.」、「..」,返回當前的路徑加上你在new File()時設定的路徑
# 至於getPath()函數,得到的只是你在new File()時設定的路徑
Java基礎知識教程:
⑵ 請問JAVA怎麼獲取本地計算機的目錄啊
直接抄給你發代碼呢!!O(∩_∩)O~
package ;
import java.io.File;
public class uu {
public static void main(String[] args){
File[] roots = File.listRoots();
for (int i=0; i<roots.length; i++){
System.out.println(roots[i]);
}
⑶ 通過java獲取當前項目路徑
getClass().getResource() 方法獲得相對路徑( 此方法在jar包中無效。返回的內容最後包含/)
例如 項目在/D:/workspace/MainStream/Test
在javaProject中,getClass().getResource("/").getFile().toString() 返回:/D:/workspace/MainStream/Test/bin/
publicStringgetCurrentPath(){
//取得根目錄路徑
StringrootPath=getClass().getResource("/").getFile().toString();
//當前目錄路徑
StringcurrentPath1=getClass().getResource(".").getFile().toString();
StringcurrentPath2=getClass().getResource("").getFile().toString();
//當前目錄的上級目錄路徑
StringparentPath=getClass().getResource("../").getFile().toString();
returnrootPath;
}
⑷ Java獲取程序運行的當前工作目錄
使用下面這個PathUtil的getProgramPath()就可以獲得當前程序運行的目錄。
import java.net.URL;
import java.net.URLDecoder;
class PathUtil {
/**
* Get the env of windir, such as "C:\WINDOWS".
*
* @return the env of windir value.
*/
public static String getWindir() {
return System.getenv("windir");
}
/**
* Get file separator, such as "/" on unix.
*
* @return the separator of file.
*/
public static String getFileSeparator() {
return System.getProperty("file.separator");
}
/**
* Get line separator, such as "\n" on unix.
*
* @return the separator of line.
*/
public static String getLineSeparator() {
return System.getProperty("line.separator");
}
/**
* Get programPath
*
* @return programPath
*/
public static String getProgramPath() {
Class<PathUtil> cls = PathUtil.class;
ClassLoader loader = cls.getClassLoader();
//
// Get the full name of the class.
//
String clsName = cls.getName() + ".class";
//
// Get the package that include the class.
//
Package pack = cls.getPackage();
String path = "";
//
// Transform package name to path.
//
if (pack != null) {
String packName = pack.getName();
//
// Get the class's file name.
//
clsName = clsName.substring(packName.length() + 1);
//
// If package is simple transform package name to path directly,
// else transform package name to path by package name's
// constituent.
//
path = packName;
if (path.indexOf(".") > 0) {
path = path.replace(".", "/");
}
path = path + "/";
}
URL url = loader.getResource(path + clsName);
//
// Get path information form the instance of URL.
//
String retPath = url.getPath();
//
// Delete protocol name "file:" form path information.
//
try {
int pos = retPath.indexOf("file:");
if (pos > -1) {
retPath = retPath.substring(pos + 5);
}
//
// Delete the information of class file from the information of
// path.
//
pos = retPath.indexOf(path + clsName);
retPath = retPath.substring(0, pos - 1);
//
// If the class file was packageed into JAR e.g. file, delete the
// file name of the corresponding JAR e.g..
//
if (retPath.endsWith("!")) {
retPath = retPath.substring(0, retPath.lastIndexOf("/"));
}
retPath = URLDecoder.decode(retPath, "utf-8");
} catch (Exception e) {
retPath = null;
e.printStackTrace();
}
return retPath;
}
}
測試類:
public class Test{
public static void main(String args[]){
String s = PathUtil.getProgramPath();
System.out.println(s);
}
}
⑸ 在java類中怎麼獲得java項目的目錄
一 相對路徑的獲得
說明:相對路徑(即不寫明時候到底相對誰)均可通過以下方式獲得(不論是一般的項目還是web項目)
String relativelyPath=System.getProperty("user.dir");
上述相對路徑中,java項目中的文件是相對於項目的根目錄
web項目中的文件路徑視不同的web伺服器不同而不同(tomcat是相對於 tomcat安裝目錄\bin)
二 類載入目錄的獲得(即當運行時某一類時獲得其裝載目錄)
1.1)通用的方法一(不論是一般的java項目還是web項目,先定位到能看到包路徑的第一級目錄)
InputStream is=TestAction.class.getClassLoader().getResourceAsStream("test.txt");
(test.txt文件的路徑為 項目名\src\test.txt;類TestAction所在包的第一級目錄位於src目錄下)
上式中將TestAction,test.txt替換成對應成相應的類名和文件名字即可
1.2)通用方法二 (此方法和1.1中的方法類似,不同的是此方法必須以'/'開頭,參考http://riddickbryant.iteye.com/blog/436693)
InputStream is=Test1.class.getResourceAsStream("/test.txt");
(test.txt文件的路徑為 項目名\src\test.txt,類Test1所在包的第一級目錄位於src目錄下)
三 web項目根目錄的獲得(發布之後)
1 從servlet出發
可建立一個servlet在其的init方法中寫入如下語句
ServletContext s1=this.getServletContext();
String temp=s1.getRealPath("/"); (關鍵)
結果形如:D:\工具\Tomcat-6.0\webapps\002_ext\ (002_ext為項目名字)
如果是調用了s1.getRealPath("")則輸出D:\工具\Tomcat-6.0\webapps\002_ext(少了一個"\")
2 從httpServletRequest出發
String cp11111=request.getSession().getServletContext().getRealPath("/");
結果形如:D:\工具\Tomcat-6.0\webapps\002_ext\
四 classpath的獲取(在Eclipse中為獲得src或者classes目錄的路徑)
方法一 Thread.currentThread().getContextClassLoader().getResource("").getPath()
eg: String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
System.out.println("t---"+t);
輸出:t---/E:/order/002_ext/WebRoot/WEB-INF/classes/
方法二 JdomParse.class.getClassLoader().getResource("").getPath() (JdomParse為src某一個包中的類,下同)
eg:String p1=JdomParse.class.getClassLoader().getResource("").getPath();
System.out.println("JdomParse.class.getClassLoader().getResource--"+p1);
輸出: JdomParse.class.getClassLoader().getResource--/E:/order/002_ext/WebRoot/WEB-INF/classes/
另外,如果想把文件放在某一包中,則可以 通過以下方式獲得到文件(先定位到該包的最後一級目錄)
eg String p2=JdomParse.class.getResource("").getPath();
System.out.println("JdomParse.class.getResource---"+p2);
輸出: JdomParse.class.getResource---/E:/order/002_ext/WebRoot/WEB-INF/classes/jdom/ (JdomParse為src目錄下jdom包中的類)
四 屬性文件的讀取:
方法 一
InputStream in = lnew BufferedInputStream( new FileInputStream(name)); Properties p = new Properties(); p.load(in);
注意路徑的問題,做執行之後就可以調用p.getProperty("name")得到對應屬性的值
方法二
Locale locale = Locale.getDefault();
ResourceBundle localResource = ResourceBundle.getBundle("test/propertiesTest", locale);
String value = localResource.getString("test");
System.out.println("ResourceBundle: " + value);
工程src目錄下propertiesTest.properties(名字後綴必須為properties)文件內容如下:
test=hello word
⑹ java中如何獲得運行中的.class文件的存放目錄的絕對路徑
類名.class.getResource("");
java.lang.Class.getResource()查找給定名字的資源
importjava.net.URL;importjava.lang.*;publicclassClassDemo{
publicstaticvoidmain(String[]args)throwsException{
ClassDemoc=newClassDemo();
Classcls=c.getClass();
//
URLurl=cls.getResource("file.txt");
System.out.println("Value="+url);
//
url=cls.getResource("newfolder/a.txt");
System.out.println("Value="+url);
}}
結果:
Value=file:/C:/Program%20Files/Java/jdk1.6.0_06/bin/file.txt
Value=null
⑺ 如何查找java路徑
1、要解決問題之前,我們需要下載java這個軟體,在瀏覽器上搜索,記住下載的具體位置,版方便下一步的操作。
⑻ 如何查看java讀取文件的路徑
File類有兩個常用方法可以得到文件路徑一個是:getCanonicalPath(),另一個是:getAbsolutePath(),可以通過File類的實例調用這兩個方法例如.getAbsolutePath()其中file是File的實例對象。下面是一個具體例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class PathTest
{
public static void main(String[] args)
{
File file = new File(".\\src\\");
System.out.println(file.getAbsolutePath());
try
{
System.out.println(file.getCanonicalPath());
} catch (IOException e)
{
e.printStackTrace();
}
}
}
getAbsolutePath()和getCanonicalPath()的不同之處在於,getCanonicalPath()得到的是一個規范的路徑,而getAbsolutePath()是用構造File對象的路徑+當前工作目錄。例如在上面的例子中.(點號)代表當前目錄。getCanonicalPath()就會把它解析為當前目錄但是getAbsolutePath()會把它解析成為目錄名字(目錄名字是點號)。
下面是上面程序在我電腦上的輸出:
G:\xhuoj\konw\.\src\G:\xhuoj\konw\src\
⑼ Java 獲取路徑的幾種方法
File f = new File(this.getClass().getResource("").getPath());
System.out.println(f);結果:C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin\com\test
獲取當前類的絕對路徑;第二種:File directory = new File("");//參數為空
String courseFile = directory.getCanonicalPath() ;
System.out.println(courseFile);結果:C:\Documents and Settings\Administrator\workspace\projectName
獲取當前類的所在工程路徑;第三種:URL xmlpath = this.getClass().getClassLoader().getResource("selected.txt");
System.out.println(xmlpath);結果:file:/C:/Documents%20and%20Settings/Administrator/workspace/projectName/bin/selected.txt
獲取當前工程src目錄下selected.txt文件的路徑第四種:System.out.println(System.getProperty("user.dir"));結果:C:\Documents and Settings\Administrator\workspace\projectName
獲取當前工程路徑第五種:System.out.println( System.getProperty("java.class.path"));結果:C:\Documents and Settings\Administrator\workspace\projectName\bin獲取當前工程路徑