前提是有权限访问FTP,那么以下2种判断文件是否存在和删除文件的方法,哪种是正确的?或者都正确?其中1号都是我问了同学之后写的,有个老员工死活说我是错的,一定要用第二种,所以求助一下,是不是我错了?
判断文件是否存在:
1.
public boolean bLockExist()
    {
     File f = new File("/123.312.123.123/folder/abc.txt");
     if(!file.exists())
     {
     return false;
     }
     return true;
    }
2.
public boolean existFile(String filepath) {
TelnetInputStream fget = null;
try {
aftp.cd(this.rootDir);
fget = aftp.get(filepath);
return true;
} catch (Exception e) {
return false;
} finally {
try {
fget.close();
} catch (Exception e) {
}
}
}删除文件也是:
1.
public void DeleteLock(String remotePath)
    {
     String cmd= "dele "+remotePath+" Token.lock \r\n";
     this.ftpClient.sendServer(cmd);
    }
2.
public void deleteFile(String fileName) throws Exception {
try {
aftp.cd(this.rootDir); String[] dirList = fileName.split("/");
for (int i = 0; i < dirList.length - 1; i++) {
if (!dirList[i].equals("") && dirList[i] != null)
aftp.cd(dirList[i]);
}
aftp.sendServer("DELE " + dirList[dirList.length - 1] + "\r\n");
aftp.readServerResponse();
} catch (Exception e) {
throw new Exception("在ftp服务器上删除文件[" + fileName + "]失败" + e);
}
}
----------
顺带问一下,在FTP上创建文件是这样写吗?
创建文件:
public void CreateLock(String remotePath)
    {
     try
     {
     File file=new File(remotePath+"Token.lock");
     if(!file.exists())
     {
     file.createNewFile();
     }
     }catch (IOException ex) {
            System.out.println("Create Lock Error");
            System.out.println(ex);
        }
    }