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.

}<br />

}<br />

in = new CipherInputStream(in, des);<br />

}<br />

catch (GeneralSecurityException e) {<br />

throw new <strong>IO</strong>Exception(e.getMessage());<br />

}<br />

}<br />

if (deflated) {<br />

in = new InflaterInputStream(in);<br />

}<br />

else if (gzipped) {<br />

in = new GZIPInputStream(in);<br />

}<br />

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

InputStreamReader isr = new InputStreamReader(in, inputEncoding);<br />

OutputStreamWriter osw = new OutputStreamWriter(out, outputEncoding);<br />

int c;<br />

while ((c = isr.read()) != -1) {<br />

osw.write(c);<br />

}<br />

isr.close();<br />

osw.close();<br />

There's one new method in this class. An overloaded variant of dump() can be invoked to<br />

dump a text file in a particular encoding. This method accepts an input encoding string and an<br />

output encoding string as arguments. These are used to form readers and writers that interpret<br />

the bytes read from the file and written onto the output stream. Output encoding is optional. If<br />

it's omitted, the platform's default encoding is used.<br />

The FileViewer2 class is straightforward. Aside from using a ModePanel2 instead of a<br />

ModePanel, the only change it really requires is in the actionPerformed() method. Here you<br />

have to test whether the format is text or numeric and select the dump() method accordingly.<br />

Example 15.11 illustrates.<br />

Example 15.11. FileViewer2<br />

import javax.swing.*;<br />

import java.io.*;<br />

import com.macfaq.io.*;<br />

import com.macfaq.swing.*;<br />

import java.awt.*;<br />

import java.awt.event.*;<br />

public class FileViewer2 extends JFrame<br />

implements WindowListener, ActionListener {<br />

JFileChooser fc = new JFileChooser();<br />

JStreamedTextArea theView = new JStreamedTextArea();<br />

ModePanel2 mp = new ModePanel2();<br />

public FileViewer2() {<br />

super("FileViewer");<br />

}<br />

392

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

Saved successfully!

Ooh no, something went wrong!