0

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.

Stanley Mungai
  • 4,044
  • 30
  • 100
  • 168

5 Answers5

2

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();
}
Toon Borgers
  • 3,638
  • 1
  • 14
  • 22
  • But I have initialized the BufferedWriter Outside the recursive for loop :P – Stanley Mungai Jan 16 '14 at 09:53
  • Not really, @Stanley. In the recursion, you open another file. And so on. – Ingo Jan 16 '14 at 09:58
  • 2
    @Stanley But a new BufferedWriter will get initialised to the same file everytime the method is called. Also when it finishes the recursion if the "path" at the root has just one directory, it will not write anything to it and will flush out an empty file. Maybe you should pass the BufferedWriter in the recursive call so that the same writer instance is used by all the recursive calls. – Saket Jan 16 '14 at 09:58
  • Can you direct me to an example because I May not be getting you correctly coz what I am trying o do is not Working. – Stanley Mungai Jan 16 '14 at 10:08
1

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();
}

}

Mayur
  • 789
  • 10
  • 37
0

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

chuhx
  • 334
  • 1
  • 3
  • 14
  • Even with FLush it does not write Anything :( See My Edit – Stanley Mungai Jan 16 '14 at 09:48
  • Then you should check your write method, maybe you should add an offset and a length [link](http://docs.oracle.com/javase/7/docs/api/java/io/BufferedWriter.html#write(java.lang.String, int, int)) – chuhx Jan 16 '14 at 09:57
  • You also can try to take a PrintWriter instead of a BufferedWriter `PrintWriter p = new PrintWriter(new FileWriter(rep));` `p.println(string);` – chuhx Jan 16 '14 at 09:59
0

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();
Asfab
  • 398
  • 6
  • 11
-1

You need to do a

java.io.BufferedWriter#flush()
before the close.
Saket
  • 3,079
  • 3
  • 29
  • 48
  • Even with Flush it does not write :( – Stanley Mungai Jan 16 '14 at 09:45
  • Do you have any files larger than 1M in the directory which you are running this? I noticed that `if(size >= limit)` is filtering out all the files when I ran it. Is there any file whose length is greater than 1000000 ? – Saket Jan 16 '14 at 10:22
  • The `BufferedWriter` will receive a `flush` when it is closed. http://docs.oracle.com/javase/7/docs/api/java/io/BufferedWriter.html#close() – Viktor Seifert Jan 16 '14 at 10:37