通过io流可以把文件里的东西读到集合里,但是把集合数据修改下 或者增加些数据到集合里 却不能用输出流写到文件里去,因为write();这个函数只能写String类型的数据 假如集合是 s 用writer(s);这么是错误的 ,实在不知道怎么解决 还望高手提点 秒数采纳 这是我的原代码
import java.io.*;
import java.util.*;
class One
{
private String name;
private String age;
public One(String name, String age) {
this.name = name;
this.age = age;
}
public String toString() {
return name + " " + age;
}
}
public class Demo {
public static void main(String[] args) throws IOException {
List<One> lt = new ArrayList<One>();
BufferedReader br = new BufferedReader(new FileReader("D:\\ziYuan\\vb.txt"));
String s = null;
while ((s = br.readLine()) != null)
{
String[] s1 = s.split(",");
One one = new One(s1[0],s1[1]);
lt.add(one);
}
br.close();
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\ziYuan\\vb.txt",true));
String s2 = br1.readLine();
String[] s3 = s2.split(" ");
lt.add(new One(s3[0],s3[1]));
bw.write(lt);
}
}