以下是实现的代码
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);
}
}