java

位置:IT落伍者 >> java >> 浏览文章

java读取文件夹下的所有文件夹和文件


发布日期:2021年05月30日
 
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);

}

}      

               

上一篇:Java获取客户端代码大全

下一篇:java中http断点续传的原理(1)