public void flush() throws IOException { if (!dirty) return; FileOutputStream fos = null; OutputStreamWriter osw = null; try { fos = new FileOutputStream(filePath); osw = new OutputStreamWriter(fos, charset); // 全局数据 for (Line l : globalLines) { osw.write(l.toString()); osw.write('\n'); } // 各个块 for (Sector s : sectors) { osw.write(s.toString()); } dirty = false; } finally { // XXX 因为stream之间有一定的依赖顺序,必须按照一定的顺序关闭流 if (osw != null) osw.close(); if (fos != null) fos.close(); } }
@Override public String toString() { StringBuilder sb = new StringBuilder(); if (space0 != null) sb.append(space0); sb.append('['); if (space1 != null) sb.append(space1); if (name != null) sb.append(name); if (space2 != null) sb.append(space2); sb.append(']'); if (space3 != null) sb.append(space3); if (comment != null) sb.append(comment); sb.append('\n'); for (Line l : lines) sb.append(l.toString()).append('\n'); return sb.toString(); }