java讀取某個(gè)文件夾下的所有文件實(shí)例代碼
java讀取某個(gè)文件夾下的所有文件
學(xué)習(xí)java基礎(chǔ)知識(shí),讀取文件,讀取某個(gè)文件夾,需要讀取所有文件,如何實(shí)現(xiàn)呢,看下實(shí)例代碼。
實(shí)例代碼:
import java.io.FileNotFoundException; import java.io.IOException; import java.io.File; public class ReadFile { public ReadFile() { } /** * 讀取某個(gè)文件夾下的所有文件 */ public static boolean readfile(String filepath) throws FileNotFoundException, IOException { try { File file = new File(filepath); if (!file.isDirectory()) { System.out.println("文件"); System.out.println("path=" + file.getPath()); System.out.println("absolutepath=" + file.getAbsolutePath()); System.out.println("name=" + file.getName()); } else if (file.isDirectory()) { System.out.println("文件夾"); String[] filelist = file.list(); for (int i = 0; i < filelist.length; i++) { File readfile = new File(filepath + "\\" + filelist[i]); if (!readfile.isDirectory()) { System.out.println("path=" + readfile.getPath()); System.out.println("absolutepath=" + readfile.getAbsolutePath()); System.out.println("name=" + readfile.getName()); } else if (readfile.isDirectory()) { readfile(filepath + "\\" + filelist[i]); } } } } catch (FileNotFoundException e) { System.out.println("readfile() Exception:" + e.getMessage()); } return true; } /** * 刪除某個(gè)文件夾下的所有文件夾和文件 */ /*public static boolean deletefile(String delpath) throws FileNotFoundException, IOException { try { File file = new File(delpath); if (!file.isDirectory()) { System.out.println("1"); file.delete(); } else if (file.isDirectory()) { System.out.println("2"); String[] filelist = file.list(); for (int i = 0; i < filelist.length; i++) { File delfile = new File(delpath + "\\" + filelist[i]); if (!delfile.isDirectory()) { System.out.println("path=" + delfile.getPath()); System.out.println("absolutepath=" + delfile.getAbsolutePath()); System.out.println("name=" + delfile.getName()); delfile.delete(); System.out.println("刪除文件成功"); } else if (delfile.isDirectory()) { deletefile(delpath + "\\" + filelist[i]); } } file.delete(); } } catch (FileNotFoundException e) { System.out.println("deletefile() Exception:" + e.getMessage()); } return true; }*/ public static void main(String[] args) { try { readfile("e:/videos"); // deletefile("D:/file"); } catch (FileNotFoundException ex) { } catch (IOException ex) { } System.out.println("ok"); } }
相關(guān)文章
java中實(shí)現(xiàn)map與對(duì)象相互轉(zhuǎn)換的幾種實(shí)現(xiàn)
這篇文章主要介紹了java中實(shí)現(xiàn)map與對(duì)象相互轉(zhuǎn)換的幾種實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07注解、原生Spring、SchemaBased三種方式實(shí)現(xiàn)AOP代碼案例
這篇文章主要介紹了注解、原生Spring、SchemaBased三種方式實(shí)現(xiàn)AOP的方法介紹,文中有詳細(xì)的代碼示例,對(duì)我們的學(xué)習(xí)有一定的幫助,需要的朋友可以參考下2023-06-06Hadoop源碼分析四遠(yuǎn)程debug調(diào)試
本篇是Hadoop源碼分析系列文章第四篇,主要介紹一下Hadoop的遠(yuǎn)程debug調(diào)試步驟,后續(xù)本系列文章會(huì)持續(xù)更新,有需要的朋友可以借鑒參考下2021-09-09eclipse自動(dòng)提示和自動(dòng)補(bǔ)全功能實(shí)現(xiàn)方法
這篇文章主要介紹了eclipse自動(dòng)提示和自動(dòng)補(bǔ)全的相關(guān)內(nèi)容,文中向大家分享了二者的實(shí)現(xiàn)方法代碼,需要的朋友可以了解下。2017-09-09Java實(shí)現(xiàn)掃雷游戲詳細(xì)代碼講解
windows自帶的游戲《掃雷》是陪伴了無(wú)數(shù)人的經(jīng)典游戲,本文將利用Java語(yǔ)言實(shí)現(xiàn)這一經(jīng)典的游戲,文中的示例代碼講解詳細(xì),感興趣的可以學(xué)習(xí)一下2022-05-05Java單元測(cè)試Powermockito和Mockito使用總結(jié)
公司單元測(cè)試框架選用了Junit 4.12,Mock框架選用了Mockito和PowerMock,本文主要介紹了Java單元測(cè)試Powermockito和Mockito使用總結(jié),感興趣的可以了解一下2021-09-09