灭菌操作

时间:2013-12-10 11:59:14

标签: java serialization

我想在文件

中打印数据列表
List<TimeSheetVO> timesheetlist;
            java.sql.Date dbdateformat = null;

            String date="2013-02-06";              

                timesheetlist=new ArrayList<TimeSheetVO>();


               java.sql.Date dbdate=java.sql.Date.valueOf(date);        
                try{
                    Class.forName("com.mysql.jdbc.Driver");
                    Connection con=DriverManager.getConnection("jdbc:mysql://192.168.1.101:3306/bio_tracker_eden","eden","centris");
                    String query = "select * from ai_bio_timesheet where ATTENDANCE_DATE=?";
                    PreparedStatement   psmt=con.prepareStatement(query);
                    psmt.setDate(1,dbdate );
                    ResultSet rs=psmt.executeQuery();
                    while(rs.next())
                    {
                         timeSheetVO =new TimeSheetVO();

                        timeSheetVO.setEMP_ID(rs.getString("EMP_ID"));
                        timeSheetVO.setATTENDANCE_DATE(rs.getString("ATTENDANCE_DATE"));
                        timeSheetVO.setIN_TIME(rs.getTime("IN_TIME"));
                        timeSheetVO.setOUT_TIME(rs.getTime("OUT_TIME"));


                        timesheetlist.add(timeSheetVO);
                        File file=new File("D:/timesheet.txt");
                        ObjectOutputStream outstream=new ObjectOutputStream(new FileOutputStream(file));
                        outstream.writeObject(timesheetlist);
                        outstream.flush();
                        outstream.close();
                    }


                }catch (Exception e) {
                    e.printStackTrace();

                }

这里我从数据库中检索数据并将数据存储到列表中,该列表存储在文件中 但是文件没有存储值 你能解释一下问题所在吗? 这里的值来自数据库,但未显示在文件中 我使用下面的课程:

public class TimeSheetVO implements Serializable{


    private String EMP_ID;
    private String ATTENDANCE_DATE;
    private Time IN_TIME;
    private Time OUT_TIME;
    private Time TOTAL_HOURS;

    public String getEMP_ID() {
        return EMP_ID;
    }
    public void setEMP_ID(String eMP_ID) {
        EMP_ID = eMP_ID;
    }

    public String getATTENDANCE_DATE() {
        return ATTENDANCE_DATE;
    }
    public void setATTENDANCE_DATE(String aTTENDANCE_DATE) {
        ATTENDANCE_DATE = aTTENDANCE_DATE;
    }
    public Time getIN_TIME() {
        return IN_TIME;
    }
    public void setIN_TIME(Time iN_TIME) {
        IN_TIME = iN_TIME;
    }
    public Time getOUT_TIME() {
        return OUT_TIME;
    }
    public void setOUT_TIME(Time oUT_TIME) {
        OUT_TIME = oUT_TIME;
    }
    public Time getTOTAL_HOURS() {
        return TOTAL_HOURS;
    }
    public void setTOTAL_HOURS(Time tOTAL_HOURS) {
        TOTAL_HOURS = tOTAL_HOURS;
    }
    @Override
    public String toString() {

        return new StringBuffer().append(EMP_ID).
                append("\n").
                append(ATTENDANCE_DATE).
                append("\n").
                append(IN_TIME).
                append("\n").
                append(OUT_TIME).toString();
    }


}

这里打印文件中的值: [72013-02-0617:50:1519:19:15 ,132013-02-0619:02:1119:02:25 ,212013-02-0618:25:2218:25:22 ,282013-02-0618:25:4318:25:43 ,442013-02-0619:20:2019:41:21 ,562013-02-0617:54:0817:54:08 ]

但我想这样打印: 72013-02-0617:50:1519:19:15 132013-02-0619:02:1119:02:25 212013-02-0618:25:2218:25:22 282013-02-0618:25:4318:25:43 442013-02-0619:20:2019:41:21 562013-02-0617:54:0817:54:08 这里附加\ n但新线路未来

1 个答案:

答案 0 :(得分:0)

您的序列化工作正常,我在本地测试,您应该能够通过反序列化重新创建TimeSheetVO列表。

  

java序列化的主要目的是将对象写入   流,以便它可以通过网络传输   对象可以重建

如果要将对象的唯一内容写入文件,则不需要序列化,而是将obj.data作为纯文本写入文件。

---编辑 -

您可以将serialization代码放在while loop之外吗?

    while(rs.next())
    {
        timeSheetVO =new TimeSheetVO();

        timeSheetVO.setEMP_ID(rs.getString("EMP_ID"));
        timeSheetVO.setATTENDANCE_DATE(rs.getString("ATTENDANCE_DATE"));
        timeSheetVO.setIN_TIME(rs.getTime("IN_TIME"));
        timeSheetVO.setOUT_TIME(rs.getTime("OUT_TIME"));
        timesheetlist.add(timeSheetVO);
    }
    File file=new File("D:/timesheet.txt");
    ObjectOutputStream outstream=new ObjectOutputStream(new FileOutputStream(file));
    outstream.writeObject(timesheetlist);
    outstream.flush();
    outstream.close();