我有一个文件(file.txt),我需要清空他当前的内容,然后多次附加一些文本.
示例:file.txt当前内容为:
AAA
BBB
CCC
我想删除此内容,然后第一次追加:
DDD
第二次:
EEE
等等...
我试过这个:
// empty the current content
fileOut = new FileWriter("file.txt");
fileOut.write("");
fileOut.close();
// append
fileOut = new FileWriter("file.txt", true);
// when I want to write something I just do this multiple times:
fileOut.write("text");
fileOut.flush();
Run Code Online (Sandbox Code Playgroud)
这工作正常,但似乎效率低下,因为我打开文件2次只是为了删除当前内容.
我有一个250行的文件.我想在第128行之后插入一些文字.
我只发现我可以在文件的末尾添加一个文本
try {
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("outfilename", true)));
out.println("the text");
out.close();
} catch (IOException e) {
//oh noes!
}
Run Code Online (Sandbox Code Playgroud)
可以在这篇文章中找到如何将文本附加到Java中的现有文件中
但没有提到行号或某事.
可能重复:
如何将文本附加到Java中的现有文件中
我想将数据添加到文本文件中.所以它是一个接一个......所以像这样:
1
2
3
<add more here>
Run Code Online (Sandbox Code Playgroud)
但我不希望删除文件中的文本.这是我正在使用atm的代码,但它取代了文件中的内容.有人可以告诉我如何做我问的问题.谢谢.
FileWriter fstream = new FileWriter("thefile.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write("blabla");
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud) 我目前正在制作一个应用程序,用户可以在其中添加多个帐户。登录信息存储在文本文件中。我需要一种方法让用户添加新帐户,而 BufferedWriter 不会覆盖其他帐户的信息。有什么建议么?
package mainFiles.fileManagers;
import java.io.*;
public class NewTXT {
public static void nFile( String a , String b ) throws IOException {
File f = new File("mainFiles\\storage\\accountinfo.txt");
FileWriter writer = new FileWriter("mainFiles\\storage\\accountinfo.txt");
BufferedWriter bWriter = new BufferedWriter(writer);
f.createNewFile();
f.canWrite();
bWriter.newLine();
bWriter.write(a + "?" + b );
bWriter.newLine();
bWriter.close();
}
}
Run Code Online (Sandbox Code Playgroud) import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class test3 {
public static void main(String[] args) {
//write
try {
FileWriter fw = new FileWriter("C:\\Users\\Danny\\Desktop\\Credits.txt");
PrintWriter pw = new PrintWriter (fw);
pw.println("This is just some test data");
pw.close();
}
catch (IOException e){
System.out.println("Error!");
}
//read
try {
FileReader fr = new FileReader("C:\\Users\\Danny\\Desktop\\Credits.txt");
BufferedReader br = new BufferedReader (fr);
String str;
while ((str = br.readLine()) != null ) {
System.out.println(str + "\n");
}
br.close();
}
catch (IOException e){ …Run Code Online (Sandbox Code Playgroud) 我试着编写一个java代码,它将为任何目录创建一个.txt文件.并且,代码会将一些字符串写入文件并将文本保存到该文件中.我写了一个可以写入文件的代码,但它无法将文本保存到我的文件中.每次运行我的代码时,新文本都会覆盖现有文本.
这是我的代码:
package com.mahbub.file_object;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class WriteFile {
private static String content = "I am a text message!!";
private static BufferedWriter bw;
public static void main(String[] args) {
File file = new File("D:/test.txt");
try {
FileWriter fw = new FileWriter(file);
bw = new BufferedWriter(fw);
bw.write("I am 2nd line");
bw.newLine();
bw.write(content);
bw.close();
System.out.println("done!!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我希望每个文本行都会一个接一个地添加到文件中而不会覆盖.谁能帮我?提前致谢!!
我正在尝试在 Java 上做一个应用程序,我需要在文件中保存 ArrayLists。我正在寻找如何做到这一点,我找到了下一个代码:
ArrayList<Expediente> al = new ArrayList<Expediente>();
Expediente exp = new Expediente("Expediente data");
try{
File file = new File("Pathname");
FileOutputStream ofs = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(ofs);
oos.writeObject(al);
oos.close();
ofs.close();
} catch (IOException e) {
System.err.println("Error!");
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我现在的问题是,如果我想保存相同的 ArrayList 但使用另一个数据,或者如果我重新打开文件,文件中的第一个数据将被覆盖。
有人能告诉如何将这些新数据附加到文件中吗?
谢谢你。
我在使用Java写入文件时遇到问题.我想写入.txt文件,但是当程序写入任何内容时,它会在写入之前删除.我不想要它.我在使用flw.append之前使用了flw.write,但没有任何改变.你能帮助我吗 ?
public void list(String path) throws IOException {
FileWriter flw=new FileWriter(path);
flw.append("----------------------------------------------List---------------------------------------------\n");
for (int i = 0; i < athCtr - 1; i++) {
flw.append("Author:" + athr[i].getId() + "\t"
+ athr[i].getName() + "\t" + athr[i].getUniv() + "\t"
+ athr[i].getDepart() + "\t" + athr[i].getEmail()+"\n");
if (athr[i].getArtCtr() != 0) {
for (int j = 0; j < athr[i].getArtCtr(); j++) {
flw.append("+" + athr[i].getArticle(j) + ":");
for (int k = 0; k < artCtr; k++) {
if (art[k].getPaperId().equals(athr[i].getArticle(j))) {
flw.append("\t" + art[k].getName() …Run Code Online (Sandbox Code Playgroud)