显示远程目录/Jtree中的所有文件 [英] Display remote directory/all files in Jtree

查看:150
本文介绍了显示远程目录/Jtree中的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用JSCH检索文件/文件夹并将其填充到JTree中时遇到了一些问题. 在JSCH中使用:

i have some problem using JSCH to retrieve files/folders and populate them in JTree. In JSCH to list files using :

向量列表= channelSftp.ls(path);

Vector list = channelSftp.ls(path);

但是我需要以java.io.File类型列出.这样我就可以获取absolutePath和fileName, 而且我不知道如何以java.io.File类型进行检索.

But i need that lists as java.io.File type. So i can get absolutePath and fileName, And i don't know how to retrieve as java.io.File type.

这是我的代码,我尝试将其用于本地目录.

Here is my code, and i try it work for local directory.

public void renderTreeData(String directory, DefaultMutableTreeNode parent, Boolean recursive) {
        File [] children = new File(directory).listFiles(); // list all the files in the directory
        for (int i = 0; i < children.length; i++) { // loop through each
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(children[i].getName());
            // only display the node if it isn't a folder, and if this is a recursive call
            if (children[i].isDirectory() && recursive) {
                parent.add(node); // add as a child node
                renderTreeData(children[i].getPath(), node, recursive); // call again for the subdirectory
            } else if (!children[i].isDirectory()){ // otherwise, if it isn't a directory
                parent.add(node); // add it as a node and do nothing else
            }
        }
    }

请帮助我,谢谢:)

推荐答案

尝试一下(远程服务器中的Linux):

Try this (linux in remote server):

public static void cargarRTree(String remotePath, DefaultMutableTreeNode parent) throws SftpException { 
    //todo: change "/" por remote file.separator
    Vector<ChannelSftp.LsEntry> list = sftpChannel.ls(remotePath); // List source directory structure.
    for (ChannelSftp.LsEntry oListItem : list) { // Iterate objects in the list to get file/folder names.       
        DefaultMutableTreeNode node = new DefaultMutableTreeNode(oListItem.getFilename());
        if (!oListItem.getAttrs().isDir()) { // If it is a file (not a directory).
            parent.add(node); // add as a child node
        } else{
            if (!".".equals(oListItem.getFilename()) && !"..".equals(oListItem.getFilename())) {
                parent.add(node); // add as a child node
                cargarRTree(remotePath + "/" + oListItem.getFilename(), node); // call again for the subdirectory
            }
        }
    }
}

您可以按以下方式调用此方法:

After you can invoque this method as:

DefaultMutableTreeNode nroot = new DefaultMutableTreeNode(sshremotedir);                
try {
    cargarRTree(sshremotedir, nroot);
} catch (SftpException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
} 
yourJTree = new JTree(nroot);

这篇关于显示远程目录/Jtree中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆