我想把D:/Test里的所有文件的路径打印出来,其中D:/Test 里有a.txt;b.txt子目录123 子目录里有c.txt;d.txt,我写了以下程序 在屏幕上打印能把D:/Test下的a.txt;b.txt 和 D:/Test/123/c.txt与D:/Test/123/d.txt 但是输出到一个文本中却无法输出D:/Test/123/c.txt与D:/Test/123/d.txt 这是为什么 代码如下 解决后马上散分
public class Test4
{
public static void main(String[] args) throws IOException
{
File folder = new File("D:/Test");
findTxtFile(folder);
}
public static void findTxtFile(File folder) throws IOException
{
ArrayList list = new ArrayList();
BufferedWriter out = new BufferedWriter(new FileWriter("c:/aa.txt"));
File files[] = folder.listFiles();
if(files == null && files.length <= 0)
{
System.out.println("没有文件");
}
else
{
for(int i = 0; i < files.length; i++)
{
File file = files[i];
if(file.isDirectory())
{
findTxtFile(file);
System.out.println(file.getName());
}
else
{
System.out.println(file.getPath());
out.write(file.getPath()+"\n");
}
}
out.close();
}
}
}
public class Test4
{
public static void main(String[] args) throws IOException
{
File folder = new File("D:/Test");
findTxtFile(folder);
}
public static void findTxtFile(File folder) throws IOException
{
ArrayList list = new ArrayList();
BufferedWriter out = new BufferedWriter(new FileWriter("c:/aa.txt"));
File files[] = folder.listFiles();
if(files == null && files.length <= 0)
{
System.out.println("没有文件");
}
else
{
for(int i = 0; i < files.length; i++)
{
File file = files[i];
if(file.isDirectory())
{
findTxtFile(file);
System.out.println(file.getName());
}
else
{
System.out.println(file.getPath());
out.write(file.getPath()+"\n");
}
}
out.close();
}
}
}
解决方案 »
- JAVA SOCKET,如何在服务器端建立一个AudioFormat?
- XML节点内容追加怎么实现?将<a>xx</a>变成<a>xxyyyyy</a>,不要setText('xx'+'yyyyy')?
- Scanner的findInLine的问题!
- 如何编译没有main的java
- 关于jfreechart的问题,头大!!!
- 如何写好一个非阻塞Socket服务器程序
- 请问java函数详解的中文书有吗?
- 有谁知道,jbuilder7.0是调用了那些命令打包成EXE文件的?
- 为什么在启动RMiregistry时总出现这样的的错误?
- 请教一下,java读取txt文件如何分块读取类似图中001-005为一块的方法
- 菜鸟问题:switch语句!!!
- 帮忙看一下弹出多个JDialog的问题,谢谢了
BufferedWriter out = new BufferedWriter(new FileWriter("c:/aa.txt"));
表示在方法内部生成一个BufferedWriter类型的out对象在for循环里,有代码:findTxtFile(file);
就是说要递归调用此方法。但是要注意的是,在调用findTxtFile这个方法的时候,在新调用的方法内部又会产生一个out对象,而且指向同一个文件,这样,文件的内容会被覆盖掉,但是屏幕上输出的内容不会被覆盖,所以会产生你所说的情况。代码可以修改为:import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;public class Test4 { static BufferedWriter out;
public static void main(String[] args) throws IOException {
File folder = new File("D:/Test");
out = new BufferedWriter(new FileWriter("d:/aa.txt"));
findTxtFile(folder);
out.close(); } public static void findTxtFile(File folder) throws IOException {
File files[] = folder.listFiles();
if (files == null && files.length <= 0) {
System.out.println("没有文件");
} else {
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (file.isDirectory()) {
out.write(file.getPath() + "\n");
findTxtFile(file);
System.out.println(file.getPath());
} else {
System.out.println(file.getPath());
out.write(file.getPath() + "\n");
}
}
} }
}
改成BufferedWriter out = new BufferedWriter(new FileWriter("c:/aa.txt",true)); 就解决了 谢谢你的建议 散分