我想复制一个.txt文件中的文本,但不包括最后一行,所以我做的是,我借助一个BufferedReader来读取整个文件(除了最后一行)。所以我做的是,我读取整个文件(接受最后一行)的帮助下,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();
}
}
但是程序并没有删除旧文件,只是删除了这个文件中的所有内容,所以这个文件是空的。而且程序也没有在新文件里面写任何东西。
你需要使用相同的文件上 deleteLastInput()
办法
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
bufWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
以下代码将加载文件的内容,删除最后一行,并将修改后的内容写回同一文件。结果是原始文件,但没有最后一行。
/* 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();
}
Du musst den gesamten Dateipfad angeben. 不仅仅是 "imgendwas.txt",还有 "c:..."
你必须指定整个文件路径。用 "something.txt "代替 "c:..."。
尝试在bufWriter.write(newInhalt)之后使用bufWriter.flush()。注意,delete()返回文件是否被删除的状态。可能是程序没有适当的权限来删除文件。
Files.delete(Paths.get(your_file_name)); //should work