您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

FileOutputStream打开一个新文件,并在创建文件时将其删除

FileOutputStream打开一个新文件,并在创建文件时将其删除

File file = new File("person.txt");
boolean toWrite = false;
boolean toModify = false;
if (file.exists())
{
   if (file.length() == 0)
   {
     toWrite = true;
   }
   else 
   {
     toModify = true;
   }
}
if (toWrite || !file.exists())
{
     FileOutputStream fos = new FileOutputStream(file);
     ObjectOutputStream oos = new ObjectOutputStream(fos);
     oos.writeObject(list);
     fos.close();
     oos.close();
}
else if (toModify)
{
   FileInputStream fins = new FileInputStream(file);
   ObjectInputStream oins = new ObjectInputStream(fins);
   LinkedList<Person> temp = (LinkedList<Person>)oins.readObject();
   fins.close();
   oins.close();
   temp.add(per);
   temp.add(per2);
   temp.add(per3);
   temp.add(per4);
   FileOutputStream fos = new FileOutputStream(file);
   ObjectOutputStream oos = new ObjectOutputStream(fos);
   oos.writeObject(temp);
   fos.close();
   oos.close();
}
其他 2022/1/1 18:16:22 有345人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶