Reputation: 475
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");
}
Upvotes: 1
Views: 187
Reputation: 31194
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;
...
Upvotes: 2