23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

public SerializableZipFile(File file) throws <strong>IO</strong>Exception {<br />

this.zf = new ZipFile(file);<br />

}<br />

private void writeObject(ObjectOutputStream out) throws <strong>IO</strong>Exception {<br />

out.writeObject(zf.getName());<br />

}<br />

private void readObject(ObjectInputStream in)<br />

throws <strong>IO</strong>Exception, ClassNotFoundException {<br />

}<br />

String filename = (String) in.readObject();<br />

zf = new ZipFile(filename);<br />

public ZipEntry getEntry(String name) {<br />

return zf.getEntry(name);<br />

}<br />

public InputStream getInputStream(ZipEntry entry) throws <strong>IO</strong>Exception {<br />

return zf.getInputStream(entry);<br />

}<br />

public String getName() {<br />

return zf.getName();<br />

}<br />

public Enumeration entries() {<br />

return zf.entries();<br />

}<br />

public int size() {<br />

return zf.size();<br />

}<br />

public void close() throws <strong>IO</strong>Exception {<br />

zf.close();<br />

}<br />

public static void main(String[] args) {<br />

try {<br />

SerializableZipFile szf = new SerializableZipFile(args[0]);<br />

ByteArrayOutputStream bout = new ByteArrayOutputStream();<br />

ObjectOutputStream oout = new ObjectOutputStream(bout);<br />

oout.writeObject(szf);<br />

oout.close();<br />

System.out.println("Wrote object!");<br />

ByteArrayInputStream bin = new<br />

ByteArrayInputStream(bout.toByteArray());<br />

ObjectInputStream oin = new ObjectInputStream(bin);<br />

Object o = oin.readObject();<br />

System.out.println("Read object!");<br />

}<br />

catch (Exception e) {e.printStackTrace();}<br />

}<br />

}<br />

<strong>Java</strong> I/O<br />

253

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!