文件中的序列化数据格式

时间:2013-12-11 04:30:09

标签: java serialization

我写了一些代码,用于在文件中显示某些格式的数据

我写在下面的代码:

File file=new File("D:/timesheet.txt");

                FileWriter fw = new FileWriter(file.getAbsoluteFile());
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(timesheetlist.toString());

                bw.close();

我的代码打印的格式如下:

[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:

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 EMP_ID + ATTENDANCE_DATE + IN_TIME + OUT_TIME + "\n";
}

} 这里是影响但不影响\ n 我在文件中显示arraylist对象 请给我任何想法。

2 个答案:

答案 0 :(得分:0)

List.toString()附加开括号[..]。请改用for-each循环来编写文本

   for(TimeSheetVO time: timesheetlist){
     bw.write(time);
   }

答案 1 :(得分:0)

只需使用StringBuilder

即可
StringBuilder sb = new StringBuilder();
for (TimeSheetVO timeSheet : timesheetlist) {
    sb.append(timeSheet.toString());
    sb.append("\n");
}

File file = new File("D:/timesheet.txt");
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(sb.toString());
bw.close();

删除“\ n”:

@Override
public String toString() {
    return EMP_ID + ATTENDANCE_DATE + IN_TIME + OUT_TIME;
}