追加写入数据到文本文件

img

具体是遇到了说明问题或者什么错误,可以详细说明一下

文件追加添加方式,加一个参数设置就可以了

ObjectOutputStream ois = new ObjectOutputStream (in);
改为
ObjectOutputStream ois = new ObjectOutputStream(new FileOutputStream(file, true));

private static void writeFile(File file, TreeSet<Book> books) {
        Iterator<Book> it = books.iterator();
        String content = "";
        while (it.hasNext()) {
            Book b = it.next();
            String ss = "isbn:" + b.isbn + "  name:" + b.name + " \n";
            content += ss;
        }
        FileOutputStream fop = null;
        try {
            fop = new FileOutputStream(file);
            if (!file.exists()) {
                file.createNewFile();
            }
            byte[] contentInBytes = content.getBytes();

            fop.write(contentInBytes);
            fop.flush();
            fop.close();
            System.out.println("Done");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fop != null) {
                    fop.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }