導航:首頁 > 文件目錄 > java如何獲取文件路徑問題

java如何獲取文件路徑問題

發布時間:2024-04-09 15:43:50

1. 如何獲得當前java文件的路徑

public class Test {
public static void main(String[] args) {
String path = "Test.java";
File file = new File(path);
System.out.println(file.getAbsoluteFile());
}
}

-----
運行結果:
D:\workspaces\studyStruts2\Test.java
不加任何路徑,就是指當版前路徑
望採納權

2. 在java中怎麼獲得,本文件的路徑

File類有兩個常用方法可以得到文件路徑一個是:getCanonicalPath(),另一個是:getAbsolutePath(),可以
通過類的實例調用這兩個方法例如file.getAbsolutePath()其中file是File的實例對象。下面是一個具體例子:

publicclassPathTest
{
publicstaticvoidmain(String[]args)
{
Filefile=newFile(".\src\");
System.out.println(file.getAbsolutePath());
try
{
System.out.println(file.getCanonicalPath());
}catch(IOExceptione)
{
e.printStackTrace();
}
}
}

getAbsolutePath()和getCanonicalPath()的不同之處在於,getCanonicalPath()得到的是一個規范的
路徑,而getAbsolutePath()是用構造File對象的路徑+當前工作目錄。例如在上面的例子中.(點號)代表當前目錄。
getCanonicalPath()就會把它解析為當前目錄但是getAbsolutePath()會把它解析成為目錄名字(目錄名字是點號)。

下面是上面程序在我電腦上的輸出:

G:xhuojkonw.src
G:xhuojkonwsrc

3. 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獲取當前工程路徑

4. java怎樣獲取當前目錄路徑

很多朋友都想知道java如何獲取當前目錄路徑?下面就一起來了解一下吧~

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()時設定的路徑 比如當前的路徑為 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()中的字元串參數如下: System.getProperty()參數大全 # 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
JAVA中獲取路徑 關鍵字: java中獲取路徑

1、jsp中取得路徑:

以工程名為TEST為例:
(1)得到包含工程名的當前頁面全路徑:request.getRequestURI() 結果:/TEST/test.jsp (2)得到工程名:request.getContextPath() 結果:/TEST (3)得到當前頁面所在目錄下全名稱:request.getServletPath() 結果:如果頁面在jsp目錄下 /TEST/jsp/test.jsp (4)得到頁面所在伺服器的全路徑:application.getRealPath("頁面.jsp") 結果:D:/resin/webapps/TEST/test.jsp (5)得到頁面所在伺服器的絕對路徑:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent(); 結果:D:/resin/webapps/TEST
2、在類中取得路徑: (1)類的絕對路徑:Class.class.getClass().getResource("/").getPath() 結果:/D:/TEST/WebRoot/WEB-INF/classes/pack/ (2)得到工程的路徑:System.getProperty("user.dir") 結果:D:/TEST

5. java獲取指定資源文件路徑的幾種方法

你好,提問者:

指定資源路徑的方法有兩種:

一種是絕對路徑專,一種是相對路徑。

獲取當前類的所屬在工程路徑;
Filef=newFile(this.getClass().getResource("/").getPath());
System.out.println(f);
獲取當前類的絕對路徑;
Filef=newFile(this.getClass().getResource("").getPath());
System.out.println(f);
獲取當前類的所在工程路徑;
Filedirectory=newFile("");//參數為空
StringcourseFile=directory.getCanonicalPath();
System.out.println(courseFile);
獲取當前工程src目錄下selected.txt文件的路徑:
URLxmlpath=this.getClass().getClassLoader().getResource("selected.txt");
System.out.println(xmlpath);

6. JAVA中怎樣獲得一個文件的全路徑

如果想要獲得當前文件中的文件名只需要String [] fileName = file.list();就可以了。如果要包括文件中的文件名就可以用遞歸的方式。下面是兩個具體的實現。
其中public static String [] getFileName(String path)是只得到當前文件中的文件名。public static void getAllFileName(String path,ArrayList<String> fileName)是包括當前文件及其子文件的文件名。

7. java中獲取文件路徑的幾種方式

File的getPath方法得到相對路徑 getAbsolutePath方法得到絕對路徑

舉個例子
String fileName = "yourfile.txt";

File aFile = new File(fileName);//這里可以把路徑拼在fileName前面 可以用相對路徑 也可以用絕對 注意分隔符
System.out.println(aFile.getPath()); //相對路徑
System.out.println(aFile.getAbsolutePath()); //絕對路徑

具體的東西在這里 http://teamojiao.iteye.com/blog/446615

閱讀全文

與java如何獲取文件路徑問題相關的資料

熱點內容
新版本vk10001p 瀏覽:13
現在哪裡開設了編程課 瀏覽:230
漫一網路培訓學校有哪些 瀏覽:752
酷狗app如何在電腦同步播放 瀏覽:668
雲浮微信群 瀏覽:344
公司文件打不開 瀏覽:267
LOL的設置文件在哪裡 瀏覽:254
線上開庭是什麼app 瀏覽:526
新中大軟體怎麼設置文件夾 瀏覽:807
git刪除文件夾 瀏覽:353
皇室戰爭怎麼升級競技場 瀏覽:447
雅虎統計工具 瀏覽:741
edgepdf文件分類 瀏覽:901
cad為什麼捕捉不到外部參照文件 瀏覽:935
重慶一共有多少個網站 瀏覽:34
k8s配置文件env創建失敗 瀏覽:197
編程序在電腦上叫做什麼 瀏覽:92
qq閱讀可賺 瀏覽:21
怎樣查找web儲存文件圖片 瀏覽:681
人口檔案資料庫包括什麼信息 瀏覽:709

友情鏈接