gpt4 book ai didi

Java.io.File 没有按应有的方式删除 File

转载 作者:行者123 更新时间:2023-12-02 08:39:31 26 4
gpt4 key购买 nike

我想复制 .txt 文件中的文本,但不复制最后一行。所以我所做的是,在 BufferedReader 的帮助下读取整个文件(接受最后一行)。然后我删除旧文件并创建新文件。然后我尝试将复制的文本写入新文件中。

public class BufferedIO {
public BufferedWriter bufWriter;
public BufferedReader bufReader;
private StringBuilder inhalt;
private File file;
private String inhaltString;
private int anzZeichen;

public BufferedIO(String Speicheradresse) {

try {

file = new File(Speicheradresse); //file object gets initialized with already existing File "Stundenzettel.txt"

bufWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));

bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

// the next 8 are there to count how many Characters are inside the File "Stundenzettel.txt"
inhalt = new StringBuilder("");
inhaltString = bufReader.readLine();

bufReader.mark(anzZeichen);

while(inhaltString != null) {
inhalt.append(inhaltString).append("\n");
inhaltString = bufReader.readLine();
}

anzZeichen = inhalt.length();

}
catch(IOException exc) {
System.out.println("IOException... Well.. That might be a problem.");
}

}
//function where the problem is situated
public void deleteLastInput() throws IOException {

bufReader.close();
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

StringBuilder inhaltKopie = new StringBuilder("");
String newInhalt;
//everyLine has 150 character, so as soon as there aren't more than 150 character left, the Reader stops to read the File.
while(anzZeichen > 150) {
inhaltKopie.append(bufReader.readLine()).append("\n");
anzZeichen -= 150;
}
//String newInhalt is initialized with the copied Text
newInhalt = inhaltKopie.toString();

//right here I close the bufferedReader and the BufferedWriter so that I can delete the file
bufReader.close();
bufWriter.close();

//old file gets deleted
file.delete();

//creating the new File
file.createNewFile();

//initializing the BufferedWriter + bufferedReader to the new File
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream("Resources/Stundenzettel.txt")));
bufWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("Resources/Stundenzettel", true)));

//bufWriter writes the copied Text inside the new File
bufWriter.write(newInhalt);

//not so important for this problem:
anzZeichen = newInhalt.length();
}
}

但是程序不会删除旧文件,它只是删除该文件中的所有内容,因此该文件为空。而且该程序不会在新文件中写入任何内容。

最佳答案

以下代码将加载文件的内容,删除最后一行并将修改后的内容写回同一文件。结果是原始文件,但没有最后一行。

/* Required imports.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
*/
Path path = Paths.get("path", "to", "your", "file");
try {
List<String> lines = Files.readAllLines(path);
int count = lines.size();
if (count > 0) {
String removed = lines.remove(count - 1);
}
Files.write(path, lines, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
}
catch (IOException xIo) {
xIo.printStackTrace();
}

关于Java.io.File 没有按应有的方式删除 File,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61463840/

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