Write trigger file once all files have been moved to unix

Sahilkhan :

I would like to move all the files to unix and once all the files have been moved, write a trigger file to unix. Below is the code:-

  public static void download(CloudFileDirectory root, 
  CloudFileDirectory backup) throws StorageException, URISyntaxException, 
  FileNotFoundException {

    ResultSegment<ListFileItem> list = 
  root.listFilesAndDirectoriesSegmented();
    for (ListFileItem item : list.getResults()) {
        URI uri = item.getUri();
        String path = uri.getPath();
        String localPath = localRootDirPath;
        String itemName = new File(path).getName();

            CloudFile file = root.getFileReference(itemName);
            file.download(new FileOutputStream(localRootDirPath+itemName));
            CloudFile backupFile = backup.getFileReference(itemName);
            backupFile.startCopy(file);
            file.delete();

            file.download(new 
     FileOutputStream(localRootDirPath+"ok_business"));
        }
     }

Problem above is that, it creates a trigger file 'ok_business' after the first file is move to unix but I want to create only once all the files have been moved.

Rafael :

Move call

file.download(new 
     FileOutputStream(localRootDirPath+"ok_business"));

out of your for-loop.

You are calling it in the loop, and after the first iteration it is being created as you have described.

Instead of code:

for (ListFileItem item : list.getResults()) {
        URI uri = item.getUri();
        String path = uri.getPath();
        String localPath = localRootDirPath;
        String itemName = new File(path).getName();

            CloudFile file = root.getFileReference(itemName);
            file.download(new FileOutputStream(localRootDirPath+itemName));
            CloudFile backupFile = backup.getFileReference(itemName);
            backupFile.startCopy(file);
            file.delete();

            file.download(new 
     FileOutputStream(localRootDirPath+"ok_business"));
 }

Use this approach. Move CloudFile file file declaration outside of the for-loop. And move the last expression outside from the for-loop.

CloudFile file;
for (ListFileItem item : list.getResults()) {
        URI uri = item.getUri();
        String path = uri.getPath();
        String localPath = localRootDirPath;
        String itemName = new File(path).getName();

            file = root.getFileReference(itemName);
            file.download(new FileOutputStream(localRootDirPath+itemName));
            CloudFile backupFile = backup.getFileReference(itemName);
            backupFile.startCopy(file);
            file.delete();
 }

   // preventing Null Pointer Exception for cases, when file is not initialized
   if (file!=null) {
       file.download(new FileOutputStream(localRootDirPath+"ok_business"));
   }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=12567&siteId=1