package jihe;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
/*
* 取出F盘下全部.java文件的文件路径保存到java.txt文件中
*
*/
public class Test15 {
public static void main(String[] args) throws IOException {
File file = new File("F:\\");
getFile(file);
}
//获取文件
public static void getFile(File file) throws IOException
{
if (file == null) {
return;
}
//文件筛选
File[] files = file.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
File f = new File(dir,name);
if (f.isFile() && name.endsWith(".java")) {
return true;
}
//如果是文件夹则递归
if(f.isDirectory())
{
try {
getFile(f);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
});
//往java.txt里面写,并追加内容
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("java.txt"),true));
//具体写的是筛选出的文件的绝对路径
for (File file2 : files) {
if (file2 == null || bw == null) {
return;
}
bw.write(file2.getAbsolutePath());
bw.newLine();
bw.flush();
}
bw.close();
}
}
|
|