</pre><pre name="code" class="javascript">public static void main(String[] args) throws IOException {
readFile("E:\\testFilePath");
deleteFile("E:\\testFilePath");
}
/**
* 获取文件路径,包括子文件夹下文件路径
* @param filePath
* @return
*/
public static boolean readFile(String filePath) {
File file = new File(filePath);
if (!file.isDirectory()) {
System.out.println("文件地址:" + file.getAbsolutePath());
} else if (file.isDirectory()) {
String[] fileList = file.list();
for (int i = 0; i < fileList.length; i++) {
File readfilePath = new File(filePath + "\\" + fileList[i]);
if (!readfilePath.isDirectory()) {
System.out.println("文件地址:" + readfilePath.getAbsolutePath());
} else if (readfilePath.isDirectory()) {
readFile(filePath + "\\" + fileList[i]);
}
}
}
return true;
}
/**
* 删除文件夹下所有文件,包括子文件夹下文件
* @param delpath
* @return
*/
public static boolean deleteFile(String delpath) {
File filePath = new File(delpath);
if (!filePath.isDirectory()) {
filePath.delete();
} else if (filePath.isDirectory()) {
String[] fileList = filePath.list();
for (int i = 0; i < fileList.length; i++) {
File delfile = new File(delpath + "\\" + fileList[i]);
if (!delfile.isDirectory()) {
System.out.println("文件地址:"+delfile.getAbsolutePath());
delfile.delete();
System.out.println("删除文件成功");
} else if (delfile.isDirectory()) {
deleteFile(delpath + "\\" + fileList[i]);
}
}
filePath.delete();
}
return true;
}
还没有评论,来说两句吧...