導航:首頁 > 編程語言 > java目錄字元串

java目錄字元串

發布時間:2023-09-20 04:08:31

java中如何獲取文件的路徑並賦給一個字元串變數

importjava.in.File

Filefile=newFile("text.java");

StringfilePath=file.getAbsolutePath();

就這樣用,

publicStringgetAbsolutePath()返回此抽象路徑名的絕對路徑名字元串。

如果此抽象路徑名已經是絕對路徑名,則返回該路徑名字元串,這與getPath()方法一樣。如果此抽象路徑名是空抽象路徑名,則返回當前用戶目錄的路徑名字元串,該目錄由系統屬性user.dir指定。否則,使用與系統有關的方式解析此路徑名。在UNIX系統上,根據當前用戶目錄解析相對路徑名,可使該路徑名成為絕對路徑名。在MicrosoftWindows系統上,根據路徑名指定的當前驅動器目錄(如果有)解析相對路徑名,可使該路徑名成為絕對路徑名;否則,可以根據當前用戶目錄解析它。

返回:

絕對路徑名字元串,它與此抽象路徑名表示相同的文件或目錄

getPath

publicStringgetPath()將此抽象路徑名轉換為一個路徑名字元串。所得字元串使用默認名稱分隔符分隔名稱序列中的名稱。

返回:

此抽象路徑名的字元串形式(相對路徑)

getName

publicStringgetName()返回由此抽象路徑名表示的文件或目錄的名稱。該名稱是路徑名名稱序列中的最後一個名稱。如果路徑名名稱序列為空,則返回空字元串。

返回:

此抽象路徑名表示的文件或目錄的名稱;如果路徑名的名稱序列為空,則返回空字元串

要API的話,我給你發(中文api),留個郵箱就行

http://docs.oracle.com/javase/6/docs/api/(英文的api)

⑵ java截取路徑字元串--得文件名

//舉例:
StringfName="G:\Java_Source\navigation_tigra_menu\demo1\img\lev1_arrow.gif";

//方法一:

FiletempFile=newFile(fName.trim());

StringfileName=tempFile.getName();

System.out.println("方法一:fileName="+fileName);

//方法二:

fName=fName.trim();

//fileName=fName.substring(fName.lastIndexOf("/")+1);
//或者
fileName=fName.substring(fName.lastIndexOf("\")+1);

System.out.println("方法二:fileName="+fileName);

//方法三:

fName=fName.trim();

Stringtemp[]=fName.split("\\");/**split裡面必須是正則表達式,"\"的作用是對字元串轉義*/

//temp[]=[G:,Java_Source,navigation_tigra_menu,demo1,img,lev1_arrow.gif]
System.out.println("temp[]="+Arrays.toString(temp));
fileName=temp[temp.length-1];

System.out.println("方法三:fileName="+fileName);

⑶ java代碼實現從路徑字元串中獲取文件名稱

這道題主要就是利用了String類的split()方法,進行多次調用,已經幫你實現了,代碼如版下:

public class Test{
public static void main(String[] args){
String str = "c:/win/good/aaa.txt;d:/win/good/bbb.txt;c:/win/cccc.txt;";
//得到路權徑數組
String[] fileRoot = str.split(";");
String[] fileName = null;
for(int i = 0;i < fileRoot.length;i++){
if(fileRoot[i] != null){
fileName = fileRoot[i].split("/");
//得到最終需要的文件名
System.out.println (fileName[fileName.length-1]);
}
}

}
}

閱讀全文

與java目錄字元串相關的資料

熱點內容
什麼app能看香港房 瀏覽:590
喪屍電視劇哪個app看 瀏覽:196
電腦版上傳視頻找不到文件在哪 瀏覽:164
javastringtemplate 瀏覽:594
聯想啟天m7300能升級內存嗎 瀏覽:698
精雕怎麼打開圖片文件 瀏覽:161
ug編程鏜孔g76如何使用 瀏覽:719
7歲到18歲少兒編程學什麼 瀏覽:413
mac文件怎麼列印 瀏覽:756
農葯登記許可證查詢哪個網站 瀏覽:857
素材站用什麼網站系統 瀏覽:173
ug如何用鉸刀編程 瀏覽:647
三國志版本介紹 瀏覽:260
情侶用的定位app有哪些 瀏覽:244
怎麼清楚蘋果手機的健身數據 瀏覽:988
淘寶美工教程自學網 瀏覽:452
父子2006未刪減版本 瀏覽:872
包頭誰賣蘋果6 瀏覽:504
化工企業停工有哪些文件通知要求 瀏覽:878
下載軟體後文件夾在桌面 瀏覽:397

友情鏈接