问题
I have this Method that Lists the Files in a Directory and I need to write the same to a file. Apparently my
System.out.println(); is able to list the files and their Sizes and the Dates they were Modified. But My bufferedWriter Does not write Anything in the File. Here is My Method;
public void walk( String path, int limit ) throws IOException {
File root = new File( path );
File[] list = root.listFiles();
File rep = new File("report.txt");
SimpleDateFormat sdf = new SimpleDateFormat("MMM/dd/yyyy HH:mm:ss");
if (list == null) return;
long size;
BufferedWriter bw = new BufferedWriter(new FileWriter(rep));
for ( File f : list ) {
size = f.length()/1000/1000;
if ( f.isDirectory() ) {
walk( f.getAbsolutePath(), limit );
}
else {
if(size >= limit){
System.out.println( "File:" + f.getAbsoluteFile() + " " + size + "MB Last Modified Date: " + sdf.format(f.lastModified()));
bw.write("File:" + f.getAbsoluteFile() + " " + size + "MB Last Modified Date: " + sdf.format(f.lastModified()) + "\n");
}
}
}
bw.close();
}
What Am I Missing? I need to write the Out to the File report.txt but the file is empty.
回答1:
I think it's because you're trying to open multiple buffered writers to the same file when it's calling itself through recursion. Try creating your writer outside of this method and pass it in as a parameter.
Example
public void myCallingMethod() throws IOException{
File rep = new File("report.txt");
BufferedWriter bw = new BufferedWriter(new FileWriter(rep));
walk("my/path", 4, bw);
bw.close();
}
回答2:
try to call the flush method after you called the write methode like:
bw.write("File:" + f.getAbsoluteFile() + " " + size + "MB Last Modified Date: " + sdf.format(f.lastModified()) + "\n");
bw.flush();
The flush method flushes your stream to your file
回答3:
Here's the code which might solve your problem. I tried the same.
public class example {
public static void main(String[] args) throws IOException {
// Directory path here
String path = "C:\\";
SimpleDateFormat sdf = new SimpleDateFormat("MMM/dd/yyyy HH:mm:ss");
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
File file = new File("report.txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
long size;
int limit = 2;
for (File f : listOfFiles) {
size = f.length() / 1000 / 1000;
if (size >= limit) {
System.out.println("File:" + f.getAbsoluteFile() + " " + size
+ "MB Last Modified Date: "
+ sdf.format(f.lastModified()));
bw.write("File:" + f.getAbsoluteFile() + " " + size
+ "MB Last Modified Date: "
+ sdf.format(f.lastModified()));
}
}
bw.close();
}
}
回答4:
Define a variable like this
StringBuilder fileData = new StringBuilder();
And replace
bw.write("File:" + f.getAbsoluteFile() + " " + size + "MB Last Modified Date: " + sdf.format(f.lastModified()) + "\n");
bw.flush();
With
fileData.append("File:").append(f.getAbsoluteFile()).append(" ").append(size)
.append("MB Last Modified Date: ").append(sdf.format(f.lastModified()))
.append("\n") ;
And after the for
loop write fileData
to file
bw.write(fileData.toString());
bw.close();
回答5:
You need to do a
java.io.BufferedWriter#flush()before the close.
来源:https://stackoverflow.com/questions/21158142/unable-to-write-to-file-using-bufferedwriter