title: List all folders in a directory without changing order even when new files are added
body: <p>How to list all folders in a directory without changing order even when new files are added, in a file using Java? When I run the program it is going to an infinite loop with "Not checked" comment which I put in order to look whether it is checking the file. </p>
  
  <pre><code>    try
      {
          BufferedWriter bufferedWriter = null;
          bufferedWriter = new BufferedWriter(new FileWriter("d://my.txt"));
          BufferedReader br = new BufferedReader(new FileReader("d://my.txt"));
  
          int i=1;
          File f=new File("D:/Moviezzz");
          File[] fi=f.listFiles();
          for(File fil:fi)
          {
              if(!fil.isHidden() &amp;&amp; (fil.isDirectory() || fil.isFile()))
              {
                  int s=i++;
  
                  String files = fil.getName();
  
                  String thisLine;
                  while(null != (thisLine="\t"+br.readLine()))
                  {
                      String exist[]=thisLine.split("\t");
                      //ensure that it is already written in file 
                      if(exist[0].equals(files))
                      {
                          System.out.println("Checked");
                      }
                      //ensure that it is not written in file 
                      else
                      {
                          System.out.println("Not Checked");
                      }
                  }
                  bufferedWriter.write(s+"\t"+files);
                  bufferedWriter.newLine();
                  bufferedWriter.flush();
                  System.out.print("yes");
  
                  // bufferedWriter.write(s+" "+files);
              }    
              br.close();
              //Construct the BufferedWriter object
              System.out.println("Succ");
          }
      }
      catch(Exception ex)
      {
          System.out.println("failed");  
      }
  </code></pre>