import java.io.File;
public class Search
{
public static void main(String[]args)
{
Search s=new Search();
File f=new File("D:\\");
s.dir(f);
}
public void dir(File f)
{
File[]name=f.listFiles();
for(int i=0;i<name.length;i++)
{
if(name[i].isFile())
{
System.out.println(name[i]);
}
else if(name[i].isDirectory())
{
dir(name[i]);
}
}
}
}
这是我写的弄出所有D盘文件名字,想把结果写进txt中,该如何实现?求教!!!
------解决方案--------------------------------------------------------
查询,写入都帮你搞定了,给分吧
- Java code
package test;import java.io.File;import java.io.FileOutputStream;public class Search { StringBuffer str = new StringBuffer(); public static void main(String[] args) { try { Search s = new Search(); s.search(new File("D:\\")); String name = s.str.toString(); File file = new File("D:\\name.txt"); FileOutputStream fos = new FileOutputStream(file); fos.write(name.getBytes()); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } public void search(File file) { if (file.isFile()) { str.append(file.getName()); } else { File[] files = file.listFiles(); for (File f : files) { search(f); } } }}
------解决方案--------------------------------------------------------
命令行
dir /s/b d: >> f:\files.txt