The Java implementation reads and deletes files under folders
- 2020-04-01 03:53:23
- OfStack
The Java implementation reads and deletes files under folders
package test.com;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ReadFile {
public ReadFile() {
}
public static boolean readfile(String filepath) throws FileNotFoundException, IOException {
try {
File file = new File(filepath);
if (!file.isDirectory()) {
//System. The out. Println (" file ");
// System.out.println("path=" + file.getPath());
// System.out.println("absolutepath=" + file.getAbsolutePath());
System.out.println(file.getName());
} else if (file.isDirectory()) {
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(readfile.getName());
} else if (readfile.isDirectory()) {
readfile(filepath + "\" + filelist[i]);
}
}
}
} catch (FileNotFoundException e) {
System.out.println("readfile() Exception:" + e.getMessage());
}
return true;
}
public static void main(String[] args) {
try {
readfile("C:\Users\SW\Desktop\SKJ_H25 � are \004_RCAG\003_SKJ");
// deletefile("D:/file");
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
}
System.out.println("ok");
}
}
Method 2:
package otherstudy;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
public class TestReadFile {
String getProjectPath(){
//Get a path that looks like "/d:/${workspace}/${projectName}/WebRoot/ web-inf /classes/"
String path=this.getClass().getResource("/").getPath();
//Extract the project path from the path string
path=path.substring(1, path.indexOf("WEB-INF/classes"));
System.out.println(" Project path: "+path);
return path;
}
public static void main(String[] args) {
TestReadFile trf=new TestReadFile();
String xmlPath = trf.getProjectPath()+ "testDocs";
try {
readAllFile(xmlPath);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean readAllFile(String filepath)
throws FileNotFoundException, IOException {
try {
File file = new File(filepath);
if (!file.isDirectory()) {
System.out.println("n File information: ");
System.out.println("t Relative paths =" + file.getPath());
System.out.println("t An absolute path =" + file.getAbsolutePath());
System.out.println("t The file name =" + file.getName());
} else if (file.isDirectory()) {
System.out.println("n List of files in the folder: ");
File[] fileList = file.listFiles();
for (int i = 0; i < fileList.length; i++) {
File readfile = fileList[i];
if (!readfile.isDirectory()) {
System.out.println("nt Relative paths =" + readfile.getPath());
System.out.println("t An absolute path =" + readfile.getAbsolutePath());
System.out.println("t The file name =" + readfile.getName());
} else if (readfile.isDirectory()) {
readAllFile(fileList[i].getPath());
}
}
}
} catch (FileNotFoundException e) {
System.out.println("readfile() Exception:" + e.getMessage());
}
return true;
}
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");
File[] fileList = file.listFiles();
for (int i = 0; i < fileList.length; i++) {
File delfile = fileList[i];
if (!delfile.isDirectory()) {
System.out.println(" Relative paths =" + delfile.getPath());
System.out.println(" An absolute path =" + delfile.getAbsolutePath());
System.out.println(" The file name =" + delfile.getName());
delfile.delete();
System.out.println(" File deleted successfully ");
} else if (delfile.isDirectory()) {
deleteFile(fileList[i].getPath());
}
}
file.delete();
}
} catch (FileNotFoundException e) {
System.out.println("deletefile() Exception:" + e.getMessage());
}
return true;
}
}
The above is all the content of this article, I hope you can enjoy it.