gpt4 book ai didi

java - 无法使用delete()删除文本文件

转载 作者:太空宇宙 更新时间:2023-11-04 13:15:35 25 4
gpt4 key购买 nike

我正在尝试将数据从旧文本文件传输到新文本文件。虽然下面的代码能够成功传输,但它不会删除旧的文本文件。我可以知道为什么会这样吗?

private void dataTransfer(String oldFilePath, String newFilePath) {

byte[] buffer = new byte[10000];
try {
FileInputStream fileInput = new FileInputStream(oldFilePath);
BufferedInputStream bufferedInput = new BufferedInputStream(fileInput);
FileOutputStream fileOutput = new FileOutputStream(newFilePath);
BufferedOutputStream bufferedOutput = new BufferedOutputStream(fileOutput);
while(true) {
int length = fileInput.read(buffer);
if(length == -1) {
break;
} else {
bufferedOutput.write(buffer);
bufferedOutput.flush();
}
}
fileInput.close();
bufferedInput.close();
fileOutput.close();
bufferedOutput.close();
File oldFile = new File(oldFilePath);
oldFile.delete();
} catch (IOException e) {
e.printStackTrace();
System.out.println(ERROR_TRANSFER_DATA);
}

}

最佳答案

更新JRE和JDK,确保您拥有该文件的权限。尝试使用您创建的文件。

另外,为 SecurityException 添加一个 catch block

关于java - 无法使用delete()删除文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33563936/

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