最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

java遞歸打印目錄樹

2022-07-14 17:28 作者:虛云幻仙  | 我要投稿

/**
* 通過遞歸創(chuàng)建/打印目錄樹
*/

public class TestFile2 {
? ?public static boolean createDirOrFile(File f,boolean file){
? ? ? ?if (file){
? ? ? ? ? ?if (f.isFile()){
? ? ? ? ? ? ? ?return false;
? ? ? ? ? ? ? ?//如果路徑已存在 返回false創(chuàng)建操作未執(zhí)行
? ? ? ? ? ?}
? ? ? ? ? ?File upper = f.getParentFile();
? ? ? ? ? ?if (upper.isDirectory()){
? ? ? ? ? ? ? ?//如果父目錄存在則創(chuàng)建該文件
? ? ? ? ? ? ? ?try {
? ? ? ? ? ? ? ? ? ?f.createNewFile();
? ? ? ? ? ? ? ?} catch (IOException e) {
? ? ? ? ? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?return true;
? ? ? ? ? ?}
? ? ? ? ? ?createDir(upper);
? ? ? ? ? ?//如果不存在父目錄則調(diào)用 createDir()方法創(chuàng)建父目錄 之后再創(chuàng)建文件
? ? ? ? ? ?try {
? ? ? ? ? ? ? ?f.createNewFile();
? ? ? ? ? ?} catch (IOException e) {
? ? ? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ? ? ?}
? ? ? ? ? ?return true;
? ? ? ?}

? ? ? ?//如果想創(chuàng)建的不是文件 file為false 即創(chuàng)建目錄
? ? ? ?//下面的語句不需要else括起來 如果file為true執(zhí)行完上面語句已經(jīng)return了不會向下執(zhí)行

? ? ? ?if (f.isDirectory()){
? ? ? ? ? ?//判斷目標目錄是否存在
? ? ? ? ? ?return false;
? ? ? ?}
? ? ? ?File upper = f.getParentFile();
? ? ? ?if (upper.isDirectory()){
? ? ? ? ? ?f.mkdir();
? ? ? ? ? ?return true;
? ? ? ?}
? ? ? ?createDir(upper);
? ? ? ?//同樣判斷父目錄是否存在 存在即創(chuàng)建f目錄 不存在則創(chuàng)建父目錄之后再創(chuàng)建f
? ? ? ?f.mkdir();
? ? ? ?return true;
? ?}

? ?private static void createDir(File dir){
? ? ? ?//該方法為類的方法static在類內(nèi)部進行調(diào)用 不需要供外部使用 所以private 內(nèi)部使用不需要查看結(jié)果 所以void
? ? ? ?//因為createDirOrFile()方法內(nèi)已經(jīng)判定目錄dir不存在了 這里不需要再次判斷

? ? ? ?File upperDir = dir.getParentFile();
? ? ? ?if (upperDir.isDirectory()){
? ? ? ? ? ?//判斷這一層目錄的父目錄是否存在
? ? ? ? ? ?//.exists()無法區(qū)分目錄和文件 只要路徑一致都會true

? ? ? ? ? ?dir.mkdir();
? ? ? ?}else{
? ? ? ? ? ?createDir(upperDir);
? ? ? ? ? ?//遞歸 如果父目錄不存在則調(diào)用本方法創(chuàng)建父目錄的父目錄 如果還不存在則再往上一層創(chuàng)建 直到某一層判斷父目錄存在后創(chuàng)建子目錄結(jié)束方法 前一層createDir()才繼續(xù)向下進行
? ? ? ? ? ?dir.mkdir();
? ? ? ?}
? ?}
? ?public static void main(String[] args) {
? ? ? ?File mp4 = new File("C:/abc/def/ghi/m.mp4");
? ? ? ?System.out.println(createDirOrFile(mp4,true));
? ? ? ?for (int i =1;i<5;i++){
? ? ? ? ? ?createDirOrFile(new File("C:/abc/def/p"+i+".jpg"),true);
? ? ? ?}

? ?}
}

class TestFile3{
? ?public static void printFileList(File f){
? ? ? ?//打印文件/目錄樹
? ? ? ?if (!f.exists()){
? ? ? ? ? ?System.out.println("目標不存在");
? ? ? ?}else {
? ? ? ? ? ?printFileList(f,0);
? ? ? ?}
? ?}
? ?private static void printFileList(File f,int n){
? ? ? ?//private私有 外部通過printFileList(File f)調(diào)用 免得傳n=0
? ? ? ?for (int i = 0;i<n;i++){
? ? ? ? ? ?System.out.print("-");
? ? ? ? ? ?//用n來區(qū)分目錄樹的層級 每深入一層就多打印一個 "-"
? ? ? ?}
? ? ? ?System.out.println(f.getName());
? ? ? ?File[] subList = f.listFiles();
? ? ? ?//.listFiles() 如果f的路徑 denote象征 一個目錄 則返回包含該目錄下所有文件的File[]數(shù)組 如果f不是目錄則返回null 如果是空目錄則返回File[0]
? ? ? ?if (subList != null){
? ? ? ? ? ?for (File subListFile:subList){
? ? ? ? ? ? ? ?//for-each循環(huán)
? ? ? ? ? ? ? ?printFileList(subListFile,n+1);
? ? ? ? ? ? ? ?//遞歸 每深入一層都使n++ 下一層同樣經(jīng)過打印文件名、判斷是否目錄、是目錄再深入一層 直到某一層是文件不執(zhí)行printFileList方法并結(jié)束這一層
? ? ? ? ? ?}
? ? ? ?}

? ?}

? ?public static void main(String[] args) {
? ? ? ?printFileList(new File("C:/abc"));
? ?}

? ?public static void printDirAndMp4(File f,int n){
? ? ? ?if (f.isDirectory()||f.getName().toLowerCase().endsWith(".mp4")){
? ? ? ? ? ?//忽略大小寫 先全部變?yōu)樾?再匹配.mp4
? ? ? ? ? ?for (int i = 0;i<n;i++){
? ? ? ? ? ? ? ?System.out.print("-");
? ? ? ? ? ?}
? ? ? ? ? ?System.out.println(f.getName());
? ? ? ?}
? ? ? ?//如果不是目錄或者mp4則不打印
? ? ? ?File[] subList = f.listFiles();
? ? ? ?//.listFiles()返回數(shù)組File[]
? ? ? ?if (subList != null){
? ? ? ? ? ?for (File subListFile:subList){
? ? ? ? ? ? ? ?//遍歷子目錄
? ? ? ? ? ? ? ?printDirAndMp4(subListFile,n+1);
? ? ? ? ? ? ? ?//遞歸 逐層遞進打印所有目錄和mp4
? ? ? ? ? ?}
? ? ? ?}
? ?}

? ?public static void printJpgOnly(File f){
? ? ? ?if (f.isDirectory()){
? ? ? ? ? ?File[] subList = f.listFiles();
? ? ? ? ? ?if (subList != null){
? ? ? ? ? ? ? ?for (File subListFile:subList){
? ? ? ? ? ? ? ? ? ?printJpgOnly(subListFile);
? ? ? ? ? ? ? ? ? ?//遞歸
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ?} else if (f.getName().toLowerCase().endsWith(".jpg")) {
? ? ? ? ? ?//.endsWith() 路徑字符串以.jpg結(jié)尾 即為jpg文件
? ? ? ? ? ?System.out.println(f.getPath());
? ? ? ?}
? ? ? ?//如果既不是目錄也不是jpg文件 跳過if和elif語句塊結(jié)束方法
? ?}
}

java遞歸打印目錄樹的評論 (共 條)

分享到微博請遵守國家法律
怀柔区| 兴隆县| 保山市| 通州市| 汽车| 清河县| 商水县| 伊通| 车险| 贵定县| 贵南县| 通河县| 雷波县| 高唐县| 连城县| 渝中区| 张家口市| 成安县| 肇东市| 视频| 奈曼旗| 丰城市| 武隆县| 广东省| 永丰县| 梅州市| 景德镇市| 兴仁县| 绿春县| 晋宁县| 德阳市| 吴川市| 池州市| 佛冈县| 休宁县| 金乡县| 沂水县| 青川县| 霍山县| 依兰县| 蓬莱市|