//序列化类 package Test; import java.io.Serializable; import java.util.Map; import javax.swing.text.SimpleAttributeSet; class InfoSerialize implements Serializable{ String m_Name; String m_Time; String m_Content; Map<Integer, String> m_Icons; SimpleAttributeSet m_SimpleAttributeSet; public InfoSerialize( String name , String time , String content , Map<Integer, String> icons , SimpleAttributeSet simpleAttributeSet ) { // TODO Auto-generated constructor stub m_Name = name; m_Time = time; m_Content = content; m_Icons = icons; m_SimpleAttributeSet = simpleAttributeSet; } } //处理类 package Test; 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.io.Serializable; import java.util.ArrayList; import java.util.Arrays; import java.util.Map; import javax.swing.JOptionPane; import javax.swing.text.SimpleAttributeSet; public class WORRecordFile { private int m_tempfileLength = 20; private String m_Str_Date; private String m_Str_ID; private String m_filePath; private File tempFile; private ObjectOutputStream oos; //每20条消息记录到记录文件中。 private static int curNum = 0; /** * @param tempfileLength 用于指定缓存文件的大小 * @param date 指定保存的目录的日期名 * @param ID 指定id * @param filePath 文件路径就是存放日期文件的路径 */ public WORRecordFile(int tempfileLength ,String date , String ID ,String filePath){ m_tempfileLength = tempfileLength; m_Str_Date = date; m_Str_ID = ID; m_filePath = filePath; try { tempFile = File.createTempFile("temp", ".txt", null); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); JOptionPane.showMessageDialog(null, "创建临时文件失败"); } } /**采用默认的1024*4B * @param date * @param ID * @param filePath */ public WORRecordFile(String date , String ID ,String filePath){ m_Str_Date = date; m_Str_ID = ID; m_filePath = filePath; try { tempFile = File.createTempFile("temp", ".info", null); System.out.println(tempFile.getAbsolutePath().toString()); oos = new ObjectOutputStream(new FileOutputStream(tempFile) ); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); JOptionPane.showMessageDialog(null, "创建临时文件失败"); } } /**每条消息都要调用一次这个函数。 * @param name * @param time * @param content * @param icons * @param simpleAttributeSet * @return */ public boolean writeToTempFile( String name , String time , String content , Map<Integer, String> icons , SimpleAttributeSet simpleAttributeSet){ try { InfoSerialize is = new InfoSerialize(name, time, content, icons, simpleAttributeSet); //满了,清空并且写入 if(curNum>=m_tempfileLength){ oos.flush(); writeToRecFile(); FileOutputStream fs = new FileOutputStream(tempFile); fs.write("".getBytes()); fs.close(); } oos.writeObject(is); //当达到满了后,再关闭oos; } catch (Exception e) { // TODO: handle exception JOptionPane.showMessageDialog(null, "写入临时文件出错"); return false; } curNum++; return true; } /** * 用于当点击关闭按钮,而tempFile又没满的时候,调用关闭。 */ public void closeOos(){ try { oos.flush(); writeToRecFile(); oos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); JOptionPane.showMessageDialog(null, "关闭OOS流出错"); } } public void writeToRecFile(){ try { FileInputStream In = new FileInputStream(tempFile); //采用追加的形式 File file = new File(m_filePath+File.separator+m_Str_Date+File.separator+m_Str_ID+File.separator+"serialData.info" ); try { if (!file.exists()) { file.getParentFile().mkdirs(); file.createNewFile(); } } catch (Exception e) { // TODO: handle exception JOptionPane.showMessageDialog(null, "创建文件失败"); return; } FileOutputStream Out = new FileOutputStream( file , true); int c; while ((c = In.read()) != -1) { Out.write(c); } In.close(); Out.close(); } catch (Exception e) { // TODO: handle exception JOptionPane.showMessageDialog(null, "写入存储文件出错"); return ; } return; } public ArrayList<InfoSerialize> readFromRecFile(){ File file = new File(m_filePath+File.separator+m_Str_Date+File.separator+m_Str_ID+File.separator+"serialData.info" ); ArrayList<InfoSerialize> infoArray = null; try { ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file)); InfoSerialize infoSerialize ; infoSerialize = (InfoSerialize)ois.readObject(); while(( infoSerialize = (InfoSerialize)ois.readObject())!=null){ infoArray.add(infoSerialize); } } catch (Exception e) { // TODO Auto-generated catch block JOptionPane.showMessageDialog(null, "读取记录文件出错"); e.printStackTrace(); return null; } return infoArray; } }