求代码:显示当前目录下所有java文件内容,并写入collection.txt文件中,并
求代码:显示当前目录下所有java文件内容,并写入collection.txt文件中,并加入行号...
求代码:显示当前目录下所有java文件内容,并写入collection.txt文件中,并加入行号
展开
2个回答
展开全部
import java.io.*;
public class Collection
{
public static void main(String[] args) throws Exception
{
final String F=".\\collection.txt";
FW=new FileWriter(new File(F));
Collection.ProcessDirectory(new File("."));
Collection.FW.flush();
Collection.FW.close();
}
private static void ProcessDirectory(File d) throws Exception
{
File[] ds=null;
Collection.ProcessJavaFile(d);
ds=d.listFiles(Collection.DFilter);
for(int i=0;i<ds.length;i++)
{
Collection.ProcessDirectory(ds[i]);
}
}
private static void ProcessJavaFile(File d) throws Exception
{
String line=null;
LineNumberReader lnr=null;
File[] fs=d.listFiles(Collection.FNFilter);
for(int i=0;i<fs.length;i++)
{
lnr=new LineNumberReader(new FileReader(fs[i]));
Collection.FW.write(fs[i].getCanonicalPath()+"\r\n");
System.out.println(fs[i].getCanonicalPath());
while(null!=(line=lnr.readLine()))
{
Collection.FW.write(""+lnr.getLineNumber()+" "+line+"\r\n");
System.out.println(""+lnr.getLineNumber()+" "+line);
}
Collection.FW.write("\r\n");
System.out.println();
}
}
private static FileWriter FW;
private static FilenameFilter FNFilter=new FilenameFilter()
{
public boolean accept(File dir,String name)
{
return name.endsWith(".java");
}
};
private static FileFilter DFilter=new FileFilter()
{
public boolean accept(File pathname)
{
return pathname.isDirectory();
}
};
}
public class Collection
{
public static void main(String[] args) throws Exception
{
final String F=".\\collection.txt";
FW=new FileWriter(new File(F));
Collection.ProcessDirectory(new File("."));
Collection.FW.flush();
Collection.FW.close();
}
private static void ProcessDirectory(File d) throws Exception
{
File[] ds=null;
Collection.ProcessJavaFile(d);
ds=d.listFiles(Collection.DFilter);
for(int i=0;i<ds.length;i++)
{
Collection.ProcessDirectory(ds[i]);
}
}
private static void ProcessJavaFile(File d) throws Exception
{
String line=null;
LineNumberReader lnr=null;
File[] fs=d.listFiles(Collection.FNFilter);
for(int i=0;i<fs.length;i++)
{
lnr=new LineNumberReader(new FileReader(fs[i]));
Collection.FW.write(fs[i].getCanonicalPath()+"\r\n");
System.out.println(fs[i].getCanonicalPath());
while(null!=(line=lnr.readLine()))
{
Collection.FW.write(""+lnr.getLineNumber()+" "+line+"\r\n");
System.out.println(""+lnr.getLineNumber()+" "+line);
}
Collection.FW.write("\r\n");
System.out.println();
}
}
private static FileWriter FW;
private static FilenameFilter FNFilter=new FilenameFilter()
{
public boolean accept(File dir,String name)
{
return name.endsWith(".java");
}
};
private static FileFilter DFilter=new FileFilter()
{
public boolean accept(File pathname)
{
return pathname.isDirectory();
}
};
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询