无法删除 java 中的 zip 文件
Unable to delete a zip file in java
我正在将几个 zip 文件从目录上传到服务器,上传成功后我将删除所有这些 zip 文件。
当我评论我的上传代码删除时工作正常,但是当我取消评论上传代码时,删除 zip 文件失败。
我参考了 SO 提出的各种问题,但其中 none 对我有所帮助。
谁能帮我解决这个问题,下面是我的代码,
上传代码
FileWriter fw = new FileWriter("log.txt");
ChannelSftp ccha = null;
Session se = null;
try {
JSch s = new JSch();
se = s.getSession("user", "hostname", 22);
se.setPassword("passwd");
se.setConfig("StrictHostKeyChecking", "no");
se.connect();
ccha = (ChannelSftp)se.openChannel("sftp");
ccha.connect();
ccha.cd("dummy");
File folder = new File("folders");
int count = 1;
for(File file : folder.listFiles()) {
try {
ccha.put(new FileInputStream(file), file.getName());
try {
ccha.ls(file.getName());
System.out.println("success");
}
catch(Exception e) {
System.out.println("fail");
fw.append("failed for - " + file.getName() + " -- " + e.getMessage() + "\n");
}
}
catch(Exception e) {
fw.append("Error" + e.getMessage() + "\n");
}
}
}
catch(Exception e) {
fw.append("session error " + e.getMessage() + "\n");
}
finally {
fw.close();
ccha.disconnect();
se.disconnect();
}
删除代码
File f = new File(path);
if(f.isDirectory()) {
ArrayList<String> allFiles = new ArrayList<String>();
getFiles(f, files); //A simple recursive function which returns all the files in the folder
for(String fpath : files)
new File(fpath).delete();
f.delete();
}
else
f.delete();
对此的任何参考也会有很大帮助!
您可以使用 ForceDelete. Add apache-common 依赖项。
试试这个:
File f = new File(path);
Arrays.stream(f.listFiles()).forEach(file->{
try{
FileDeleteStrategy.FORCE.delete(file);
}catch (Exception ex){
}
});
您没有正确关闭资源。您打开文件进行阅读但从未在
关闭它
ccha.put(new FileInputStream(file), file.getName());
这样可以防止文件被删除。
我正在将几个 zip 文件从目录上传到服务器,上传成功后我将删除所有这些 zip 文件。
当我评论我的上传代码删除时工作正常,但是当我取消评论上传代码时,删除 zip 文件失败。
我参考了 SO 提出的各种问题,但其中 none 对我有所帮助。
谁能帮我解决这个问题,下面是我的代码,
上传代码
FileWriter fw = new FileWriter("log.txt");
ChannelSftp ccha = null;
Session se = null;
try {
JSch s = new JSch();
se = s.getSession("user", "hostname", 22);
se.setPassword("passwd");
se.setConfig("StrictHostKeyChecking", "no");
se.connect();
ccha = (ChannelSftp)se.openChannel("sftp");
ccha.connect();
ccha.cd("dummy");
File folder = new File("folders");
int count = 1;
for(File file : folder.listFiles()) {
try {
ccha.put(new FileInputStream(file), file.getName());
try {
ccha.ls(file.getName());
System.out.println("success");
}
catch(Exception e) {
System.out.println("fail");
fw.append("failed for - " + file.getName() + " -- " + e.getMessage() + "\n");
}
}
catch(Exception e) {
fw.append("Error" + e.getMessage() + "\n");
}
}
}
catch(Exception e) {
fw.append("session error " + e.getMessage() + "\n");
}
finally {
fw.close();
ccha.disconnect();
se.disconnect();
}
删除代码
File f = new File(path);
if(f.isDirectory()) {
ArrayList<String> allFiles = new ArrayList<String>();
getFiles(f, files); //A simple recursive function which returns all the files in the folder
for(String fpath : files)
new File(fpath).delete();
f.delete();
}
else
f.delete();
对此的任何参考也会有很大帮助!
您可以使用 ForceDelete. Add apache-common 依赖项。
试试这个:
File f = new File(path);
Arrays.stream(f.listFiles()).forEach(file->{
try{
FileDeleteStrategy.FORCE.delete(file);
}catch (Exception ex){
}
});
您没有正确关闭资源。您打开文件进行阅读但从未在
关闭它ccha.put(new FileInputStream(file), file.getName());
这样可以防止文件被删除。