gpt4 book ai didi

java - 读入输入流后删除文件

转载 作者:行者123 更新时间:2023-11-30 07:49:33 25 4
gpt4 key购买 nike

我正在尝试从本地存储的文件中检索视频流。读入输入流后,我试图删除该文件,但它不允许这种情况发生。我知道我需要关闭流,但我需要将此流传递给网络服务器调用。关于如何最好地解决这个问题的任何想法:

InputStream is = new FileInputStream("\\Location\\file.txt");
File f = new File("\\Location\\file.txt");

if(f.delete()) {
System.out.println("success");
} else {
System.out.println("failure");
}

最佳答案

尝试在 Finally block 上删除

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

class DeleteFile extends FileInputStream {

File file;

public DeleteFile(String s) throws FileNotFoundException {
this(new File(s));
}

public DeleteFile(File file) throws FileNotFoundException {
super(file);
this.file = file;
}

public void close() throws IOException {
try {
super.close();
} finally {
if (file != null) {
file.delete();
file = null;
}
}
}
}

关于java - 读入输入流后删除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48441485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com