|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
一开始我再 BufferedWriter bw = new BufferedWriter(new FileWriter("c:\\change\\pop.txt"));这行后面加了个true,因为怕递归会把文件覆盖掉,不对,把true去掉就对了?为啥啊
package com.xiapeixin;
import java.io.*;
import java.util.*;
public class Test99
{//1:要求对指定目录进行递归;2:获取所有.java文件;3:存储到集合中;4:写入到文件中;
public static void main(String[] args) throws Throwable
{
File file = new File("D:\\ZTesting\\");
TreeSet<File> alf = new TreeSet<File>(new MyC());
getJava(file,alf);
}
static void getJava(File dir,TreeSet<File> af) throws IOException
{
File [] fs = dir.listFiles();
for (File f:fs)
{
if (f.isDirectory())
{
getJava(f,af);
}
else if (f.getName().endsWith(".java"))
{
af.add(f);
}
}
BufferedWriter bw = new BufferedWriter(new FileWriter("c:\\change\\pop.txt"));
for (Iterator <File> i =af.iterator();i.hasNext();)
{
File fo = i.next();
bw.write(fo.getAbsolutePath());
bw.newLine();
}
bw.close();
}
}
class MyC implements Comparator<File>
{
@Override
public int compare(File o1, File o2)
{
return o1.getAbsolutePath().compareTo(o2.getAbsolutePath());
}
}
BufferedWriter bw = new BufferedWriter(new FileWriter("c:\\change\\pop.txt",true));
这行代码的意思是在pop.txt文件上追加内容,不会覆盖之前的内容。可以使用下面的代码试试:
static void getJava(File dir,TreeSet<File> af) throws IOException
{
File [] fs = dir.listFiles();
for (File f:fs)
{
if (f.isDirectory())
{
getJava(f,af);
}
else if (f.getName().endsWith(".java"))
{
af.add(f);
writerString(File f);
}
}
}
static void writerString(File f)
{
BufferedWriter bw = new BufferedWriter(new FileWriter("c:\\change\\pop.txt",true));
bw.write(f.getAbsolutePath());
bw.newLine();
bw.close();
}
|
|