Monday, December 12, 2016

Move files by using Commons VFS APIs (Virtual File System) in java





Below class Test have moveFile method to move file from d:\\temp\\server.txt" local path to shared folder path \\\\<shared_folder_path>\\<folder_name>\\

public class Test {
     public static void main(String[] args) throws org.apache.commons.vfs2.FileSystemException {
         moveFile("server", "d:\\temp\\server.txt");
        }
     public static void moveFile(String fileName, String inputFolder)
                                                 throws org.apache.commons.vfs2.FileSystemException
    {

               FileObject destn = VFS.getManager().resolveFile("\\\\<shared_folder_path>\\<folder_name>\\" + fileName +                                                                                                                                                  ".txt");

             FileObject fo = VFS.getManager().resolveFile(inputFolder);     
             fo.moveTo(destn);
             destn.close();
      }
}


Note: server.txt and \\\\<serverIp>\\<folder_name>\\ should exist before executing above code.

Above code will move the file from source to destination. Better to use for synchronized operation in you application.


No comments:

Post a Comment

Fixing yum command on linux

You may find yourself having to fix more packages. So you can just remove everything you had installed via  pip and reinstall everythin...