我正在尝试使用以下代码删除网络(Y 驱动器)驱动器上的文件
import org.apache.commons.vfs.FileSystemOptions;
import org.apache.commons.vfs.UserAuthenticator;
import org.apache.commons.vfs.auth.StaticUserAuthenticator;
import org.apache.commons.vfs.impl.DefaultFileSystemConfigBuilder;
import org.apache.commons.vfs.VFS;
import org.apache.commons.vfs.FileObject;
public class DeleteLogFile {
public static void main(String[] args) {
String filePath = "y:\\test\\test.log";
String domain = "ABCDESDX";
String userName = "abc";
String password = "xyzl@jun2013";
String remoteFilePath = filePath;
try {
UserAuthenticator auth = new StaticUserAuthenticator(domain, userName, password);
FileSystemOptions opts = new FileSystemOptions();
DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
FileObject fo = VFS.getManager().resolveFile(remoteFilePath, opts);
if (fo.exists()) {
System.out.println("FILE IS THERE");
if (fo.delete()) {
System.out.println("deleted");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
上面的程序正在删除文件,但该程序不使用用户名和密码,因为如果我输入错误的凭据,那么文件仍然会被删除
请建议此方法或替代方法来实现此要求