这个程序是获取某个盘(如C盘)的所有文件的路径。如果文件比较多的话,就会出现内存溢出,大家帮忙看看怎么优化好呢~!
package test.io;import java.io.*;
import java.text.DateFormat;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;public class IoAllDemo {
StringBuffer sbf = new StringBuffer();
int count = 0;
public static void main(String[] args) throws Exception {
IoAllDemo al = new IoAllDemo();
File newFile = new File("F:" + File.separator + "test.txt");
newFile.createNewFile();
long lm = newFile.lastModified();
Date d = new Date(lm);
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
DateFormat.MEDIUM);
String str = df.format(d);

al.getName(new File("D:\\"));
String name = al.sbf.toString();
FileOutputStream fos = new FileOutputStream(newFile);
fos.write(name.getBytes());
fos.write(str.getBytes());
fos.flush();
fos.close();

System.out.println("test.txt最后更改时间" + str);
} public void getName(File file){

if(file.isFile()){
sbf.append(count+"-->"+file.getPath()+"  ");
}
if (file.isDirectory()) {
File list[] = file.listFiles();
if (list != null) {
for (File ls : list) {
System.out.println(count+"-->"+ls);
getName(ls);
count++;
}
}
}
}}

解决方案 »

  1.   

    个人认为:
      递归时不是用一个StringBuffer连接保存,而是递归一个文件就写一次文件。
      

  2.   

    我改了一下,可是只能输出最后个路径,我想全部都输出,应该怎么改呢?
    package test.io;import java.io.*;
    import java.text.DateFormat;
    import java.util.Date;public class IoAllDemo {
    //StringBuffer sbf = new StringBuffer();
    static File newFile = new File("F:" + File.separator + "test.txt");
    static int count = 0;
    public static void main(String[] args) throws Exception {
    //IoAllDemo al = new IoAllDemo();
    //newFile.createNewFile();
    long lm = newFile.lastModified();
    Date d = new Date(lm);
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
    DateFormat.MEDIUM);
    String str = df.format(d);

    getName(new File("G:\\"));
    /*String name = al.sbf.toString();
    String b = "\\n";
    FileOutputStream fos = new FileOutputStream(newFile);
    fos.write(name.getBytes());
    fos.write(b.getBytes());
    fos.write(str.getBytes());
    fos.flush();
    fos.close();*/

    System.out.println("test.txt最后更改时间" + str);
    } public static void getName(File file)throws Exception{
    //newFile = new File("F:" + File.separator + "test.txt");

    if(file.isFile()){
    //sbf.append(count+"-->"+file.getPath()+"  ");
    FileOutputStream fos = new FileOutputStream(newFile);
    String s ;
    s = count+"-->"+file.getPath()+"  ";
    fos.write(s.getBytes());
    fos.write("\n".getBytes());
    fos.flush();
    fos.close();
    }
    if (file.isDirectory()) {
    File list[] = file.listFiles();
    if (list != null) {
    for (File ls : list) {
    System.out.println(count+"-->"+ls);
    getName(ls);
    count++;
    }
    }
    }

    }}
      

  3.   

    在main方法中创建 FileOutputStream ,并做为参数传给getName方法。getName中直接对这个流进行写操作。最后到main方法里关闭流。
      

  4.   

    不对吧~!getName方法是接收File类型的参数,按你那样的话,得把getName方法的参数类型改成Object ,不过这样会出现类转换异常,而且也不会全部写入。纠结呀~!
      

  5.   

    用Stringbuffer连接保存,递归每一个文件夹,写出的时候使用
      FileOutputStream fos = new FileOutputStream(newFile,true); 这样可以保证文件不会被重新建,被覆盖重写。同时保证内存不溢出
      

  6.   

    确实,不过当某个文件的文件很多的时候,还是会导致内存溢出(比如我的C:\的某个文件有30W个文件)
    这是我修改后的代码,大家指点指点
    package test.io;import java.io.*;
    import java.text.DateFormat;
    import java.util.Date;public class IoAllDemo {
    //StringBuffer sbf = new StringBuffer();
    static File newFile = new File("F:" + File.separator + "test.txt");
    static int count = 1;
    public static void main(String[] args) throws Exception {
    //IoAllDemo al = new IoAllDemo();
    newFile.createNewFile();
    FileOutputStream fos = new FileOutputStream(newFile,true);
    long lm = newFile.lastModified();
    Date d = new Date(lm);
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
    DateFormat.MEDIUM);
    String str = "test.txt最后更改时间" + df.format(d);

    getName(new File("C:\\"));
    fos.write(str.getBytes());
    fos.write("\n".getBytes());
    fos.flush();
    fos.close();
    System.out.println(str);
    } public static void getName(File file)throws Exception{
    //newFile = new File("F:" + File.separator + "test.txt");

    if(file.isFile()){
    //sbf.append(count+"-->"+file.getPath()+"  ");
    FileOutputStream fos = new FileOutputStream(newFile,true);
    String s = count+"-->"+file.getPath()+"   ";//获取文件路径名
    fos.write(s.getBytes());//写入
    fos.write("\n".getBytes());
    fos.flush();
    fos.close();
    count++;
    }else if (file.isDirectory()) {
    File list[] = file.listFiles();
    if (list != null) {
    //使用递归获取每个文件夹
    for (File ls : list) {
    System.out.println(count+"-->"+ls);
    getName(ls);
    }
    }
    }

    }}