0

I want to check if a directory is exist by using the notExists(Path path, LinkOption... options) and Im confused with the LinkOption.NOFOLLOW_LINKS although after I googled I still not quite get when to use it. Here are my codes:

import java.io.*; 
import java.nio.file.Files; 
import java.nio.file.*; 
import java.util.ArrayList; 

public class Test 
{ 
    public static void main(String[] args) throws IOException 
    {   
          Path source = Paths.get("Path/Source");
          Path destination = Paths.get("Path/Destination");
          ArrayList<String> files = new ArrayList<String>();
          int retry = 3;

          // get files inside source directory
          files = getFiles(source);

          // move all files inside source directory
          for (int j=0; j < files.size(); j++){
              moveFile(source,destination,files.get(j),retry);         
          }       
    } 

    // move file to destination directory
    public static void moveFile(Path source, Path destination, String file, int retry){
        while (retry>0){
            try {
                // if destination path not exist, create directory
                if (Files.notExists(destination, LinkOption.NOFOLLOW_LINKS)) {
                    // make directory  
                    Files.createDirectories(destination);  
                }

                // move file to destination path
                Path temp = Files.move(source.resolve(file),destination.resolve(file));         

                // if successfully, break
                if(temp != null){ 
                    break;
                } 
                // else, retry
                else {
                    --retry;
                } 

            } catch (Exception e){
                // retry if error occurs
                --retry;
            }
        }
    }

    // get all file names in source directory
    public static ArrayList<String> getFiles(Path source){

        ArrayList<String> filenames = new ArrayList<String>();
        File folder = new File(source.toString());
        File[] listOfFiles = folder.listFiles();   // get all files inside the source directory

        for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile()) {
                filenames.add(listOfFiles[i].getName()); // add file's name into arraylist
            } 
        }
        return filenames;
    }
} 

The result of using LinkOption.NOFOLLOW_LINKS and not using it are the same (The files are transferred to the destination). So, Im guessing for my case, i can ignore the Link option? also, in what situation will i be needing that? Thanks!

  • Post your [mre] demonstrating how you attempted to use it. what class does the `notExists()` method belong to. Then tell us what the result of that code was. That is did the directory exist or not? Did the result change if you used the NOFOLLOW_LINKS option. You can check out: https://stackoverflow.com/q/61785056/131872 which shows how to post an MRE. You can also check out my answer where I decided to use the `NOFOLLOW_LINKS`. It might help explain the difference. – camickr Jun 12 '20 at 01:25
  • whoops sorry, already edited the question, Im using **java.nio.file.Files** . The directory has not yet existed, so by using notExists(), I will create the directory when i first run the code. Also, the result for using NOFOLLOW_LINKS and using it is the same... – Aimi Umairahhh Jun 12 '20 at 01:39

1 Answers1

0

So, Im guessing for my case, i can ignore the Link option?

You can only follow a link if the link exists.

So if you are testing to make sure a directory doesn't exist, there are two outcomes.

  1. it exists, so there is no need to follow the link.
  2. it doesn't exist, so there is nothing to follow.

in what situation will i be needing that?

Did you look at my answer in the link I provided you? I tried to give a simple example.

camickr
  • 321,443
  • 19
  • 166
  • 288