gpt4 book ai didi

java - 错误,我无法删除临时文件或将其替换为原始文件java

转载 作者:行者123 更新时间:2023-12-03 07:55:57 25 4
gpt4 key购买 nike

我试图删除临时文件或将临时文件替换为原始文件。但是我不知道该怎么做。谁能帮助我或者指出我该更改哪一部分?

public class Deletion {
public static void main (String[] args) throws IOException {

final File inputFile = new File("myFile.txt");
final File tempFile = new File("myTempFile.txt");
tempFile.delete();
BufferedReader reader = null;
BufferedWriter writer = null;
try {
reader = new BufferedReader(new FileReader(inputFile));
writer = new BufferedWriter(new FileWriter(tempFile));

final String lineToRemove1 = "Apple";
final String lineToRemove2 = "Orange";
String currentLine;
while((currentLine = reader.readLine()) != null) {
// trim newline when comparing with lineToRemove
final String trimmedLine = currentLine.trim();
if (!currentLine.contains(lineToRemove1)
&& !currentLine.contains(lineToRemove2)) {
writer.write(currentLine);
writer.newLine();
}
}
}

finally {
if (reader != null) {
reader.close();
}
if (writer != null) {
writer.close();
}
}
FileChannel src = new FileInputStream(tempFile).getChannel();
FileChannel dest = new FileOutputStream(inputFile).getChannel();
dest.transferFrom(src, 0, src.size());
tempFile.delete();
}
}

最佳答案

package com.example.servingwebcontent;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.channels.FileChannel;

public class Deletion {
public static void main(String[] args) throws IOException, URISyntaxException {
System.out.println(System.getProperty("user.dir"));

FileReader reader = new FileReader("myFile.txt");
BufferedReader bufferedReader = new BufferedReader(reader);

FileWriter writer = new FileWriter("myTempFile.txt", false);

try {

final String lineToRemove1 = "Apple";
final String lineToRemove2 = "Orange";
String currentLine = bufferedReader.readLine();
while (currentLine != null) {
// trim newline when comparing with lineToRemove
final String trimmedLine = currentLine.trim();
if (!(currentLine.contains(lineToRemove1) || currentLine.contains(lineToRemove2))) {
writer.write(currentLine);
// new line
writer.write("\r\n");
}
currentLine = bufferedReader.readLine();
}
}

finally {

if (reader != null) {
reader.close();
reader = null;
bufferedReader = null;
}
if (writer != null) {
writer.close();
writer = null;

}
}

FileChannel sourceChannel = null;
FileChannel destChannel = null;
File src = new File("myTempFile.txt");
File dest = new File("myFile.txt");
try {

sourceChannel = new FileInputStream(src).getChannel();
destChannel = new FileOutputStream(dest).getChannel();
destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());

} finally {
sourceChannel.close();
destChannel.close();
src.delete();
}

}
}
试试这个代码。这里的问题是必须关闭资源(src.delete();必须在sourceChannel.close()和destChannel.close();之后),否则您将无法删除文件。
引用: Java 'file.delete()' Is not Deleting Specified File
67
该问题也可能是由于您忘记关闭任何输出流而导致的。就我而言,在删除文件之前,我正在使用该文件。但是,在文件操作中的某个地方,我忘记了关闭用于写入后来试图删除的文件的输出流。

关于java - 错误,我无法删除临时文件或将其替换为原始文件java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65574073/

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