How to list files directory/files using Apache Common vfs

16,828

List of files can be displayed by using FileObject#getChildren() method.

FileSystemOptions opts = new FileSystemOptions();
fsManager = VFS.getManager();

// List all the files in that directory.Try to give the directory path  
FileObject localFileObject=fsManager.resolveFile("ftp://"+userStr+":"+passStr+"@"+host+"/home");
FileObject[] children = localFileObject.getChildren();
for ( int i = 0; i < children.length; i++ ){
    System.out.println( children[ i ].getName().getBaseName() );
}
// End of List Files.

FileObject file = fsManager.resolveFile("ftp://"+userStr+":"+passStr+"@"+host+"/home/", opts);

My suggestion would be to Use JSCH framework which is best for SFTP operations. As this Apache Common VFS inherently used this framework.The complexcity will be greatly reduced by JSCH.

Share:
16,828
fanjavaid
Author by

fanjavaid

Updated on July 25, 2022

Comments

  • fanjavaid
    fanjavaid almost 2 years

    i am new using Apache Common vfs, I success connect to the server I already read docs but i'm stuck in this code. How i can list directory/files?

    ....
    Session session = null;
            FileSystemManager fsManager = null;
            FileSystem fs = null;
            try {
                String host = "host_here";
                int port = 22;
    
                String userStr = "user_here";
                char [] username = userStr.toCharArray();
    
                String passStr = "password_here";
                char [] password = passStr.toCharArray();
    
                session = SftpClientFactory.createConnection(host, port, username, password, null);
                //session.connect();
    
                System.out.println("Connected to the server");
    
                FileSystemOptions opts = new FileSystemOptions();
                fsManager = VFS.getManager();
                FileObject file = fsManager.resolveFile("ftp://"+userStr+":"+passStr+"@"+host+"/home/", opts);    
    
                // .... whats next i do here? .....
    
            } catch (Exception e) {
                session.disconnect();
                e.printStackTrace();
            }
    ...
    

    Please help me, Thank you before :)

  • fanjavaid
    fanjavaid about 11 years
    Thank you srinivas, Now i use JSCH :D But i wonder, how to save directory(not file) to destination directory?
  • SRy
    SRy about 11 years
    You mean create new directory or save a directory with already some files in it?
  • fanjavaid
    fanjavaid about 11 years
    Yes, like that. Its possible?