package cn.hncu.addr.dao; import java.io.EOFException; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; public class AddressFile { public static boolean writeToFile(String fileName,Object[] objs) { ObjectOutputStream out=null; try { out=new ObjectOutputStream(new FileOutputStream(fileName)); for(Object obj:objs){ out.writeObject(obj); } } catch (FileNotFoundException e) { File f=new File(fileName);//如果文件不存在就新建个文件 try { f.createNewFile(); } catch (IOException e1) { //e1.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); }finally{ if(out!=null){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } return true; } public static Object[] readFromFile(String fileName) { ObjectInputStream in=null; ArrayList<Object> list=new ArrayList<Object>(); try { in = new ObjectInputStream(new FileInputStream(fileName)); Object obj; while(true){//对象留不能用avaolable()来判断 try { obj=in.readObject(); list.add(obj); } catch (EOFException e) { break; } } } catch (Exception e) { }finally{ if(in!=null){ try { in.close(); } catch (Exception e2) { } } } Object objs[]=list.toArray(); if(objs==null){ objs=new Object[0];//防止空指针异常 } return objs; } }
数据层(数据处理)
package cn.hncu.addr.dao; import java.io.EOFException; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStre
本文来自网络,不代表站长网立场,转载请注明出处:https://www.tzzz.com.cn/html/shuju/2021/0526/6655.html