这个程序是获取某个盘(如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++;
}
}
}
}}
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++;
}
}
}
}}
递归时不是用一个StringBuffer连接保存,而是递归一个文件就写一次文件。
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++;
}
}
}
}}
FileOutputStream fos = new FileOutputStream(newFile,true); 这样可以保证文件不会被重新建,被覆盖重写。同时保证内存不溢出
这是我修改后的代码,大家指点指点
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);
}
}
}
}}