package cn.zen.io.file;
import java.io.File;
import java.io.FilenameFilter;
import org.junit.Test;
public class FileEx {
@Test
public void method_1() {
File f = new File("D:" + File.separator + "StudyIo");
// 得到D:\StudyIo目录下的所有目录和文件名字
// String[] names = f.list();
// for(String name : names){
// System.out.println(name);
// }
// 将以后缀名为.JPG的文件名列出来
String[] names = f.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".JPG");
}
});
for (String name : names) {
System.out.println(name);
}
// 得到D:\StudyIo目录下的所有目录和文件(对象)
// File[] files = f.listFiles();
// for (File file : files) {
// System.out.println(file.getName());
// }
// System.out.println(f.exists()); System.out.println(f.getPath());
// System.out.println(f.getAbsolutePath());
// System.out.println(f.isDirectory()); System.out.println(f.isFile());
}
/**
* 将目录下的文件以层级的方式显示出来
* */
@Test
public void method_2() {
File dir = new File("D:" + File.separator + "StudyIo");
viewFile(dir, 0);
}
public String getLevelStr(int level) {
StringBuffer sb = new StringBuffer();
sb.append("|--");
for (int i = 0; i < level; i++) {
sb.insert(0, "| ");
}
return sb.toString();
}
public void viewFile(File dir, int level) {
System.out.println(getLevelStr(level) + dir.getName());
level++;
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
viewFile(files[i], level);
} else {
System.out.println(getLevelStr(level) + files[i].getName());
}
}
}
}
—————————————————————————————————————————————————————————————————————————————
package cn.zen.io.file;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
public class DelFileEx {
@Test
public void method_2() {
String strDir = "D:" + File.separator + "javaenchac";
File dir = new File(strDir);
List<File> fList = new ArrayList<File>();
fileToList(dir, fList);
System.out.println("size:" + fList.size());
File txtFileName = new File(dir, "fileList.txt");
writerToTxt(fList, txtFileName.toString());
}
/**
* 将目录下所有以.java的文件都存入到List集合中去
*/
public void fileToList(File dir, List<File> fileList) {
File[] files = dir.listFiles();
for (File f : files) {
if (f.isDirectory()) {
fileToList(f, fileList);
} else {
if (f.getName().endsWith(".java")) {
fileList.add(f);
}
}
}
}
/**
* 将文件路径写到txt文本中去
*/
public void writerToTxt(List<File> fList, String txtFileName) {
BufferedWriter bufw = null;
try {
bufw = new BufferedWriter(new FileWriter(txtFileName));
for (File f : fList) {
bufw.write(f.getAbsolutePath());
bufw.newLine();
bufw.flush();
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
if (bufw != null) {
bufw.close();
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}
/**
* 删除目录的所以文件 包括目录
*/
@Test
public void method_1() {
String strDir = "D:" + File.separator + "StudyIo";
File dir = new File(strDir);
removeFile(dir);
}
public void removeFile(File dir) {
if (!dir.exists()) {
System.out.println("该目录不存在!");
return;
}
File[] files = dir.listFiles();
for (File file : files) {
if (file.isDirectory() && !file.isHidden()) {
removeFile(file);
} else {
System.out.println("file:" + file.getName() + "——>" + file.delete());
}
}
System.out.println("dir:" + dir.getName() + "——>" + dir.delete());
}
}