简体   繁体   English

如何在Java中列出远程计算机上目录的所有文件?

[英]How to list all the files of a directory which is on a remote machine in java?

I have ac code.I need to write a java code which i would then have to convert into a jar file and run from my c code.The purpose of the jar file would be to log into a remote machine and then navigate to a defined path and then list all the files in the specific directory in string format.That is i want to obtain something like String FileNames[] = {"File1","File2",...} so that then i can use the system call available for FTP log in and download to download all the files recursively. 我有一个ac代码。我需要编写一个Java代码,然后将其转换为jar文件并从我的c代码运行.jar文件的目的是登录到远程计算机,然后导航到已定义的路径,然后以字符串格式列出特定目录中的所有文件。那就是我想获取类似String FileNames [] = {“ File1”,“ File2”,...}这样的东西,以便我可以使用系统调用可用于FTP登录并下载以递归方式下载所有文件。

So how do i do that.Any idea? 那我该怎么办呢?

查看包含FTP实用程序的Apache commons-net

这就是您所需要的,看看它: http : //docs.oracle.com/javase/6/docs/api/java/io/File.html

ArrayList<String> list = new ArrayList<String>();

public void recursive_file(String path) {
File theFolder = new File(path);
File temp;
String[] theFiles = path.list();

for (int i=0; i<theFiles.length; i++) {
    theFiles[i]=path+theFiles[i];
    temp=new File(theFiles[i]);
    if (temp.isDirectory()) {
        recursive_file(theFiles[i]);
        }
    else { list.add(theFiles[i]); 
         }
    }
}

Something like this? 像这样吗 Not sure it's the most elegant solution. 不确定这是最优雅的解决方案。 You might also need to append a "/" or "\\" on your path String depending on how it's given to the method. 您可能还需要在路径字符串上附加“ /”或“ \\”,具体取决于将其赋予方法的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM