熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

java讀取文件夾下的所有文件夾和文件

2022-06-13   來源: Java核心技術 

  以下是實現的代碼


   package comborlandsampleswelcome;

import javaioFileNotFoundException;
import javaioIOException;
import javaioFile;

public class ReadFile {
public ReadFile() {}

/**
* 刪除某個文件夾下的所有文件夾和文件
* @param delpath String
* @throws FileNotFoundException
* @throws IOException
* @return boolean
*/
public static boolean deletefile(String delpath) throws FileNotFoundException
IOException {
try {

File file = new File(delpath);
if (!fileisDirectory()) {
Systemoutprintln();
filedelete();
}
else if (fileisDirectory()) {
Systemoutprintln();
String[] filelist = filelist();
for (int i = ; i < filelistlength; i++) {
File delfile = new File(delpath + \\ + filelist[i]);
if (!delfileisDirectory()) {
Systemoutprintln(path= + delfilegetPath());
Systemoutprintln(absolutepath= + delfilegetAbsolutePath());
Systemoutprintln(name= + delfilegetName());
delfiledelete();
Systemoutprintln(刪除文件成功);
}
else if (delfileisDirectory()) {
deletefile(delpath + \\ + filelist[i]);
}
}
filedelete();

}

}
catch (FileNotFoundException e) {
Systemoutprintln(deletefile() Exception: + egetMessage());
}
return true;
}

/**
* 刪除某個文件夾下的所有文件夾和文件
* @param delpath String
* @throws FileNotFoundException
* @throws IOException
* @return boolean
*/
public static boolean readfile(String filepath) throws FileNotFoundException
IOException {
try {

File file = new File(filepath);
if (!fileisDirectory()) {
Systemoutprintln(文件);
Systemoutprintln(path= + filegetPath());
Systemoutprintln(absolutepath= + filegetAbsolutePath());
Systemoutprintln(name= + filegetName());

}
else if (fileisDirectory()) {
Systemoutprintln(文件夾);
String[] filelist = filelist();
for (int i = ; i < filelistlength; i++) {
File readfile = new File(filepath + \\ + filelist[i]);
if (!readfileisDirectory()) {
Systemoutprintln(path= + readfilegetPath());
Systemoutprintln(absolutepath= + readfilegetAbsolutePath());
Systemoutprintln(name= + readfilegetName());

}
else if (readfileisDirectory()) {
readfile(filepath + \\ + filelist[i]);
}
}

}

}
catch (FileNotFoundException e) {
Systemoutprintln(readfile() Exception: + egetMessage());
}
return true;
}

public static void main(String[] args) {
try {
readfile(D:/file);
//deletefile(D:/file);
}
catch (FileNotFoundException ex) {
}
catch (IOException ex) {
}
Systemoutprintln(ok);
}


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26965.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.