-1

Im using BufferedOutputStream for writing in a file ,it writes list of files and directories in th specified driver, heres the code:

import java.io.File;
import java.nio.file.*;
import java.io.*;
import static java.nio.file.StandardOpenOption.*;
public class DirectoryReader {

static int spc_count=-1;

static void Process(File aFile)
{
String s;
Path file =
Paths.get("C:\\Java\\Files1.txt");
try
{
 OutputStream output= new 
 BufferedOutputStream(Files.newOutputStream(file,CREATE));

 spc_count++;
 String spcs = "";
 byte [] data= new byte [2048 * 2048];
 for (int i = 0; i < spc_count; i++)
 spcs += " ";
 if(aFile.isFile())
 {
 System.out.println(spcs + "[FILE] " + aFile.getPath());
 s=aFile.getPath();
 System.out.println(" " + s);
 data = s.getBytes();
 output.write(data);
 output.write(13);
 output.write(10);
 output.flush();
 }
else if (aFile.isDirectory()) {
{
System.out.println(spcs + "[DIR] " + aFile.getPath());
s=aFile.getPath();
data = s.getBytes();
output.write(data);
output.write(13);
output.write(10);
output.flush();
}
File[] listOfFiles = aFile.listFiles();
if(listOfFiles!=null) {
for (int i = 0; i < listOfFiles.length; i++)
Process(listOfFiles[i]);
} else {
System.out.println(spcs + " [ACCESS DENIED]");
}
}
output.close();
spc_count--;
}
catch(Exception e)
    {
    System.out.println("Message: " + e);
    }


}


public static void main(String[] args) {
String nam = "D:\\Notes";
File aFile = new File(nam);
Process(aFile);
}

}

but when I go to see the file , I only find the last file written and sometimes I find mixed words .

hubosh
  • 3
  • 4
  • Please provide the full program; your code snippet doesnt show how output is initialized; where data is coming from, and so on... – GhostCat Mar 23 '15 at 07:24
  • I added the full code as you asked ... – hubosh Mar 23 '15 at 07:44
  • Consider a look into Apache Commons-IO and in particular http://commons.apache.org/proper/commons-io/javadocs/api-release/org/apache/commons/io/FileUtils.html#writeStringToFile(java.io.File,%20java.lang.String) With this utility class you can much more easily access files... So you can avoid dealing with BufferedOutputStream directly. – mle Mar 23 '15 at 07:52
  • Side note: your indenting is horrible. It makes it MUCH harder to read your source code. – GhostCat Mar 23 '15 at 07:57
  • Try to stick to naming convention. http://www.oracle.com/technetwork/java/codeconventions-135099.html – Antoniossss Mar 23 '15 at 08:01

1 Answers1

0

You are using recurrent calls to your Process method. This method is creating NEW FILE upon execution, so previous content is simply overriden. I would suggest, that you should create output stream, and pass it as argument to every call Process message. This way, you will provide single output stream for storing data. And the problem will be gone.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • and I tried comparing the file names but no match was found .. I don't know why although the file name and the string from file.getName() are the same – hubosh Mar 23 '15 at 08:26
  • And that was not the point? To list all files dirs and subdirs into single file? – Antoniossss Mar 23 '15 at 08:43
  • it worked .. the error was in output.close , I closed the file at the end of the method although I'll call it again .. and of course what you said two about passing the OutputStream as an argument .. thanks alot for your help , you really did me great :) – hubosh Mar 23 '15 at 09:26