1

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.

    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() && (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");  
    }
Shan
  • 463
  • 3
  • 17
  • When are you reading anything from the `BufferedReader`? Are you actually trying to read and write from the same file simultaneously? Unless you a) are implementing a database, and b) know what you're doing, this is a bad idea. You should change your program to do this in multiple steps. First read the old file into an appropriate data structure, then make changes to the data structure (add new files while checking for duplicates), then write out the whole thing again. – millimoose May 13 '13 at 14:51
  • 2
    1) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. 2) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson May 13 '13 at 14:51

1 Answers1

2

You're getting an infinite loop, because the condition in your while loop can never be false.

while(null != (thisLine="\t"+br.readLine()))

you're essentially adding "\t" to the results of readLine(), so the value you're comparing to null will never actually be null. It will always have a \t in it.


After further research, it appears like thisLine will be be "\tnull" once you've reached the end of the stream

If I were you, I wouldn't do all that in your while loop. I'd do something more like this.

while(null != (thisLine=br.readLine()))
{
    thisLine = "\t"+ + thisLine;
    ...
Community
  • 1
  • 1