0

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.

shmosel
  • 49,289
  • 6
  • 73
  • 138
yumi
  • 153
  • 8
  • I don't know what you're trying to do, but `for(int i = 0 ; i – shmosel May 12 '17 at 04:29
  • Also, see [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – shmosel May 12 '17 at 04:30
  • hmm. next time I should explain it more further for what I plan to do with the distinct file path – yumi May 12 '17 at 07:16

2 Answers2

1

Instead of file == y2j.get(i) use equals method. Also instead of going through whole list you can use method contains that will return true if list contains element.

== in java compares if this is exacly the same object by comparing reference, but it will not distinguish between objects that look the same but are not same, ex: new String("n") == new String("n") will return false.

vip1all
  • 122
  • 11
0

If you want distinct set of file paths, then you can use Set instead of List. If you use Set then you don't have to run an extra loop for removing duplicates.

Nishesh Pratap Singh
  • 2,131
  • 2
  • 13
  • 20
  • Hi Nishesh, thanks for your comment, I found out the way to solve it but another issues pop up, try to solve it now – yumi May 12 '17 at 07:14