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 static void dumpDecimal(String filename) {<br />

}<br />

FileInputStream fin = null;<br />

byte[] buffer = new byte[16];<br />

boolean end = false;<br />

int bytesRead;<br />

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

try {<br />

fin = new FileInputStream(filename);<br />

while (!end) {<br />

bytesRead = 0;<br />

while (bytesRead < buffer.length) {<br />

int r = fin.read(buffer, bytesRead, buffer.length - bytesRead);<br />

if (r == -1) {<br />

end = true;<br />

break;<br />

}<br />

bytesRead += r;<br />

}<br />

for (int i = 0; i < bytesRead; i++) {<br />

int dec = buffer[i];<br />

if (dec < 0) dec = 256 + dec;<br />

if (dec < 10) System.out.print("00" + dec + " ");<br />

else if (dec < 100) System.out.print("0" + dec + " ");<br />

else System.out.print(dec + " ");<br />

}<br />

System.out.println();<br />

}<br />

}<br />

catch (<strong>IO</strong>Exception e) {System.err.println(e);}<br />

finally {<br />

try {<br />

if (fin != null) fin.close();<br />

}<br />

catch (<strong>IO</strong>Exception e) { }<br />

}<br />

public static void dumpHex(String filename) {<br />

FileInputStream fin = null;<br />

byte[] buffer = new byte[24];<br />

boolean end = false;<br />

int bytesRead;<br />

try {<br />

fin = new FileInputStream(filename);<br />

while (!end) {<br />

bytesRead = 0;<br />

while (bytesRead < buffer.length) {<br />

int r = fin.read(buffer, bytesRead, buffer.length - bytesRead);<br />

if (r == -1) {<br />

end = true;<br />

break;<br />

}<br />

bytesRead += r;<br />

}<br />

for (int i = 0; i < bytesRead; i++) {<br />

int hex = buffer[i];<br />

if (hex < 0) hex = 256 + hex;<br />

58

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

Saved successfully!

Ooh no, something went wrong!