欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中如何獲取文件的上級(jí)目錄

 更新時(shí)間:2022年12月12日 09:11:33   作者:南獨(dú)酌酒nvn  
這篇文章主要介紹了Java中如何獲取文件的上級(jí)目錄問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Java獲取文件的上級(jí)目錄

通過 File 對(duì)象的 getParent 方法即可實(shí)現(xiàn)

// 通過 System.getProperty("user.dir") 方式獲取到項(xiàng)目根目錄
String projectRootDirectoryPath = System.getProperty("user.dir");
System.out.println("當(dāng)前項(xiàng)目根目錄為:\t" + projectRootDirectoryPath);
// 通過 File 對(duì)象的 getParent() 方法獲取到根目錄的上級(jí)目錄
String parentPath = new File(projectRootDirectoryPath).getParent();
System.out.println("當(dāng)前項(xiàng)目根目錄的上級(jí)目錄為:\t" + parentPath);

效果截圖


在這里插入圖片描述

獲取文件的父目錄名稱和父目錄絕對(duì)地址

獲取文件的父目錄名稱

import java.io.File;
?
public class Main {
? ? public static void main(String[] args) {
? ? ?? ?//創(chuàng)建file對(duì)象
? ? ? ? File file = new File("C:/File/demo.txt");
? ? ? ? //先獲取file的父文件,再getName
? ? ? ? String strParentDirectory = file.getParentFile().getName();
? ? ? ? System.out.println("文件的上級(jí)目錄為 : " + strParentDirectory);
? ? }
}

輸出結(jié)果:
File

獲取文件的父目錄絕對(duì)地址

import java.io.File;
?
public class Main {
? ? public static void main(String[] args) {
? ? ?? ?//創(chuàng)建file對(duì)象
? ? ? ? File file = new File("C:/File/demo.txt");
? ? ? ? //先獲取file的父文件,再getName
? ? ? ? String strParentDirectory = file.getParentFile().getName();
? ? ? ? System.out.println("文件的上級(jí)目錄為 : " + strParentDirectory);
? ? }
}

輸出結(jié)果:
C:/File

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論