请问一下,java中有没直接判断ftp上文件夹下是否存在某文件的方法?通过遍历文件夹的方式判断太耗内存了

如题所述

第一个种方法 :
org.apache.commons.net.ftp.* 看这个目录下是否有你要的方法
第二种方法:

package com.soft4j.log4j;

import java.io.IOException;

import sun.net.ftp.FtpClient;

public class FtpTest
{
static String middle_ftpServer = "10.103.2.250";
static String middle_user = "ora9iftp";
static String middle_password = "ftp";
static String middle_dir = "/image/NWKPHOTO/Middle/2009/3";

public static void main(String[] args)
{
FtpClient ftpClient = new FtpClient();
try
{
ftpClient.openServer(middle_ftpServer);
ftpClient.login(middle_user, middle_password);
FtpTest ft = new FtpTest();
ft.isDirExist(ftpClient, middle_dir);
} catch (IOException e)
{
e.printStackTrace();
}

}

/** 判断Ftp目录是否存在,如果不存在则创建目录 */
public void isDirExist(FtpClient ftpClient, String dir)
{
try
{
ftpClient.cd(dir); //想不到什么好办法来判断目录是否存在,只能用异常了(比较笨).请知道的告诉我一声`
} catch (IOException e1)
{
ftpClient.sendServer("MKD " + dir + "\r\n");
try
{
ftpClient.readServerResponse();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}追问

ftpClient.changeWorkingDirectory(dir);的方法来判断目录是否存在,如果切换目录成功返回true,否则返回false。ftpClient.*中没有直接判断文件是否存在的方法,只能通过遍历目录即listFiles的方法来进行,但是这样读取目录特别耗内存,因此想看看有没有别的方法比如new File(filename).exists()之类的方法来减少内存占用。

追答

感谢 ! 你挺会想的 竟然 想到 dos命令~

温馨提示:答案为网友推荐,仅供参考