I want to compare the file path where captured by the FileVisitResult visitFile
in my arraylist. The reason for this comparison is preventing there's duplicate file path store in the arraylist since FileVisitResult visitFile
will keep on looping while the condition is true. I want to make sure all the file had just visited by the method once. So each time the FileVisitResult
method will capture the file path and store into arraylist, when the next round for the method execution, any new file path detected will compare to the file path in the arraylist, but it's still fail to do it after I search some information online,the Thread.sleep()
use to halt the program for my understanding the flow of my source code
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException
{
String f;
File file1;
Path tempPath1,tempPath2;
if(counter == 0) //first round for the execution
{
counter++;
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("The abs path of the file is"+file);
y2j.add(file); //add path to arraylist
f = file.toString();
file1 = new File(f);
if ( file1.exists()) //checking the file process whether still consume by any other process
{
RandomAccessFile statusCheck = null;
try
{
statusCheck = new RandomAccessFile(file1,"r");
if(statusCheck != null)
{
System.out.println("Not hodling by any process,clear to send for scanning");
statusCheck.close();
ambrose(); //do something in this method
}
}
catch(Exception e)
{
System.out.println("In processing : " + e.getMessage());
}
}
}
else if(counter > 0) //after first round of execution
{
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("larger than 0");
System.out.println("The abs path of the file is"+file);
for(int i = 0 ; i<y2j.size()-1;i--)
{
System.out.println("In the for loop" +y2j);
for(int k = i+1 ; k<y2j.size();k++)
{
if(file == y2j.get(k))
{
System.out.println("they are same");
}
else if(file != y2j.get(k))
{
y2j.add(file); //add path to arraylist
ambrose(); //do something in this method
}
}
}
}
return FileVisitResult.CONTINUE;
}
The abs path of the file isC:\REST API\source\abc\abc.xlsx
Not hodling by any process,clear to send for scanning
Dirty deed here
larger than 0
The abs path of the file isC:\REST API\source\abc\def.txt
larger than 0
The abs path of the file isC:\REST API\source\abc\abc.xlsx
larger than 0
The abs path of the file isC:\REST API\source\abc\def.txt
It's seen like not entering the for loop, I have spent some time on it but can't figure where is my problem.