我有一个程序,可以将人员添加到arraylist.我想要做的是将这些人添加到文本文件中,但程序会覆盖第一行,以便人员被删除.
如何告诉编译器在下一个空闲行写入?
import java.io.*;
import java.util.*;
import javax.swing.JTextArea;
public class Logic {
File file;
FileWriter fw;
FileReader fr;
BufferedWriter bw;
ArrayList<Person> person;
public Logic() {
try {
file = new File("register.txt");
if (!file.exists()) {
file.createNewFile();
}
} catch (IOException e) {
}
person = new ArrayList<Person>();
}
// Add person
public void addPerson(String name, int tele) {
person.add(new Person(name, tele));
savePerson(name, tele);
}
// Save person to external file
public void savePerson(String name, int tele) {
try {
fw = …Run Code Online (Sandbox Code Playgroud) 我正在尝试用字符串写一个文件,然后阅读它.但是,当我看到它看起来很奇怪与正方形和东西..我无法复制粘贴!
这是代码:
import java.io.*;
public class ObjectOutputStreamDemo {
public static void main(String[] args) throws ClassNotFoundException {
try {
//Now Im writing
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("fruit.dat"));
String ord[] = { "Banana", "Mango", "Apple", "Passionfruit","Orange" };
for (int i = 0; i < 5; i++) {
output.writeObject(ord[i]);
}
output.close();
//Now Im trying to read.
BufferedReader in = new BufferedReader(new FileReader("fruit.dat"));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
} catch (IOException e) {
System.out.println("Problem with file.");
} …Run Code Online (Sandbox Code Playgroud)