Monday, December 12, 2016

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






Below class Test have copyFile 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 copyFile(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);     
             destn.copyFrom(fo, Selectors.SELECT_SELF);
             destn.close();
      }
}


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

Above code will copy the file from source to destination.

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...