1.12k likes | 1.14k Views
Java I/O. In this Madness there is a Method. Outline. Stream IO (bytes) cont. Piped IO Streams CORBA IO Streams Stream IO (char) OutputStreamWriter FileWriter InputStreamReader FileReader BufferedWriter BufferedReader LineNumberReader PushbackReader CharArrayWriter CharArrayReader
E N D
Java I/O In this Madness there is a Method
Outline • Stream IO (bytes) cont. • Piped IO Streams • CORBA IO Streams • Stream IO (char) • OutputStreamWriter • FileWriter • InputStreamReader • FileReader • BufferedWriter • BufferedReader • LineNumberReader • PushbackReader • CharArrayWriter • CharArrayReader • StringWriter • StringReader • PipedWriter • PipedReader • Ramdom Access • RandomAccessFIle • The File Class • Stream IO (bytes) • FileInputStream • FileOutputStream • BufferedOutputStream • BufferedInputStream • DataOutputStrream • DataInputStream • DigestOutputStream • DigestInputStream • CheckedOutputStream • CheckedInputStream • PrintStream • PushbacktInputStream • Stream IO (bytes) cont. • ProgressMonitorInputStream • DeflaterOutputStream • InflaterInputStream • GZIPOutputStream • GZIPInputStream • ZipOutputStrream • ZipInputStream • JarOutputStream • Java Archive (jar) Utility • SequenceInputStream • AudioInputStream • ObjectOutputStream • ObjectInputStream • ByteArrayInputStream • ByteArrayInputStream
New Topic Random Access Files
Reading with a RandomAccessFile import java.io.*; public class RandomFile1 { public static void main(String[] args)throws IOException { RandomAccessFile raf = new RandomAccessFile( "HelloWorld.txt", "r"); for(int i = 0; i < raf.length(); i++) { System.out.print((char)raf.read()); } raf.close(); } } HelloWorld.txt Hi there world. Are you having a nice day? Hi there world. Are you having a nice day?
Reading and Writing a RandomAccessFile import java.io.*; public class RandomFile3 { public static void main(String[] args)throws IOException { RandomAccessFile raf = new RandomAccessFile("RandomFile", "rw"); raf.writeUTF("Hello there."); raf.writeInt(42); raf.writeDouble(3.14159d); raf.seek(0); System.out.println(raf.readUTF()); System.out.println(raf.readInt()); System.out.println(raf.readDouble()); raf.close(); } } Hello there. 42 3.14159
The 38 Methods of RandomAccessFile • void close() • Closes this random access file stream and releases any system resources associated with the stream. • FileDescriptor getFD() • Returns the opaque file descriptor object associated with this stream. • long getFilePointer() • Returns the current offset in this file. • long length() • Returns the length of this file. • int read() • Reads a byte of data from this file.
The 38 Methods of RandomAccessFile • int read(byte[] b) • Reads up to b.length bytes of data from this file into an array of bytes. • int read(byte[] b, int off, int len) • Reads up to len bytes of data from this file into an array of bytes. • boolean readBoolean() • Reads a boolean from this file. • byte readByte() • Reads a signed eight-bit value from this file. • char readChar() • Reads a Unicode character from this file. • double readDouble() • Reads a double from this file.
The 38 Methods of RandomAccessFile • float readFloat() • Reads a float from this file. • void readFully(byte[] b) • Reads b.length bytes from this file into the byte array, starting at the current file pointer. • void readFully(byte[] b, int off, int len) • Reads exactly len bytes from this file into the byte array, starting at the current file pointer. • int readInt() • Reads a signed 32-bit integer from this file. • String readLine() • Reads the next line of text from this file. • long readLong() • Reads a signed 64-bit integer from this file.
The 38 Methods of RandomAccessFile • short readShort() • Reads a signed 16-bit number from this file. • int readUnsignedByte() • Reads an unsigned eight-bit number from this file. • int readUnsignedShort() • Reads an unsigned 16-bit number from this file. • String readUTF() • Reads in a string from this file. • void seek(long pos) • Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs. • void setLength(long newLength) • Sets the length of this file.
The 38 Methods of RandomAccessFile • int skipBytes(int n) • Attempts to skip over n bytes of input discarding the skipped bytes. • void write(byte[] b) • Writes b.length bytes from the specified byte array to this file, starting at the current file pointer. • void write(byte[] b, int off, int len) • Writes len bytes from the specified byte array starting at offset off to this file. • void write(int b) • Writes the specified byte to this file. • void writeBoolean(boolean v) • Writes a boolean to the file as a one-byte value.
The 38 Methods of RandomAccessFile • void writeByte(int v) • Writes a byte to the file as a one-byte value. • void writeBytes(String s) • Writes the string to the file as a sequence of bytes. • void writeChar(int v) • Writes a char to the file as a two-byte value, high byte first. • void writeChars(String s) • Writes a string to the file as a sequence of characters.
The 38 Methods of RandomAccessFile • void writeDouble(double v) • Converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the file as an eight-byte quantity, high byte first. • void writeFloat(float v) • Converts the float argument to an int using the floatToIntBits method in class Float, and then writes that int value to the file as a four-byte quantity, high byte first. • void writeInt(int v) • Writes an int to the file as four bytes, high byte first. • void writeLong(long v) • Writes a long to the file as eight bytes, high byte first.
The 38 Methods of RandomAccessFile • void writeShort(int v) • Writes a short to the file as two bytes, high byte first. • void writeUTF(String str) • Writes a string to the file using UTF-8 encoding in a machine-independent manner.
The File Class An Abstract Pathname and File System Toolkit
The File Class • A class that replaces a Pathname • An Abstract Pathname • Answers Questions About the Pathname • Provides Tools (methods) for Manipulation
The File Class, an Abstract Pathname import java.io.*; public class RandomFile3 { public static void main(String[] args)throws IOException { RandomAccessFile raf = new RandomAccessFile("RandomFile", "rw"); raf.writeUTF("Hello there."); raf.writeInt(42); raf.writeDouble(3.14159d); raf.seek(0); System.out.println(raf.readUTF()); System.out.println(raf.readInt()); System.out.println(raf.readDouble()); raf.close(); } } import java.io.*; public class RandomFile3 { public static void main(String[] args)throws IOException { RandomAccessFile raf = new RandomAccessFile(f, "rw"); raf.writeUTF("Hello there."); raf.writeInt(42); raf.writeDouble(3.14159d); raf.seek(0); System.out.println(raf.readUTF()); System.out.println(raf.readInt()); System.out.println(raf.readDouble()); raf.close(); } } import java.io.*; public class RandomFile3 { public static void main(String[] args)throws IOException { File f = new File("RandomFile"); RandomAccessFile raf = new RandomAccessFile(f, "rw"); raf.writeUTF("Hello there."); raf.writeInt(42); raf.writeDouble(3.14159d); raf.seek(0); System.out.println(raf.readUTF()); System.out.println(raf.readInt()); System.out.println(raf.readDouble()); raf.close(); } } Abstract Pathname Object Hello there. 42 3.14159
Some of the Methods of the File Class import java.io.*; import java.util.*; public class AbstractPathname3 { public static void main(String[] args) throws IOException { File f = new File("../File/Random.txt"); System.out.println("f.getPath() = " + f.getPath()); System.out.println("f.getAbsolutePath() = " + f.getAbsolutePath()); System.out.println("f.getCanonicalPath() = " + f.getCanonicalPath()); System.out.println("f.isDirectory() = " + f.isDirectory()); System.out.println("f.isFile() = " + f.isFile()); System.out.println("f.lastModified() = " + new Date(f.lastModified())); System.out.println("f.length() = " + f.length()); System.out.println("f.canRead = " + f.canRead()); } }
Some of the Methods of the File Class import java.io.*; import java.util.*; public class AbstractPathname3 { public static void main(String[] args) throws IOException { File f = new File("../File/Random.txt"); System.out.println("f.getPath() = " + f.getPath()); System.out.println("f.getAbsolutePath() = " + f.getAbsolutePath()); System.out.println("f.getCanonicalPath() = " + f.getCanonicalPath()); System.out.println("f.isDirectory() = " + f.isDirectory()); System.out.println("f.isFile() = " + f.isFile()); System.out.println("f.lastModified() = " + new Date(f.lastModified())); System.out.println("f.length() = " + f.length()); System.out.println("f.canRead = " + f.canRead()); } } f.getPath() = ..\File\Random.txt f.getAbsolutePath() = C:\Java\File\..\File\Random.txt f.getCanonicalPath() = C:\Java\File\Random.txt f.isDirectory() = false f.isFile() = true f.lastModified() = Mon Apr 09 19:32:58 CDT 2001 f.length() = 36 f.canRead = true
Methods of the File Class • boolean canRead() • Tests whether the application can read the file denoted by this abstract pathname. • boolean canWrite() • Tests whether the application can modify to the file denoted by this abstract pathname. • int compareTo(File pathname) • Compares two abstract pathnames lexicographically. • int compareTo(Object o) • Compares this abstract pathname to another object. • boolean createNewFile() • Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.
Methods of the File Class • static File createTempFile(String prefix, String suffix) • Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name. • static File createTempFile(String prefix, String suffix, File directory) • Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name. • boolean delete() • Deletes the file or directory denoted by this abstract pathname.
Methods of the File Class • void deleteOnExit() • Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates. • boolean equals(Object obj) • Tests this abstract pathname for equality with the given object. • boolean exists() • Tests whether the file denoted by this abstract pathname exists. • File getAbsoluteFile() • Returns the absolute form of this abstract pathname. • String getAbsolutePath() • Returns the absolute pathname string of this abstract pathname.
Methods of the File Class • File getCanonicalFile() • Returns the canonical form of this abstract pathname. • String getCanonicalPath() • Returns the canonical pathname string of this abstract pathname. • String getName() • Returns the name of the file or directory denoted by this abstract pathname. • String getParent() • Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory.
Methods of the File Class • String getPath() • Converts this abstract pathname into a pathname string. • int hashCode() • Computes a hash code for this abstract pathname. • boolean isAbsolute() • Tests whether this abstract pathname is absolute. • boolean isDirectory() • Tests whether the file denoted by this abstract pathname is a directory. • boolean isFile() • Tests whether the file denoted by this abstract pathname is a normal file. • boolean isHidden() • Tests whether the file named by this abstract pathname is a hidden file.
Methods of the File Class • long lastModified() • Returns the time that the file denoted by this abstract pathname was last modified. • long length() • Returns the length of the file denoted by this abstract pathname. • String[] list() • Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname. • String[] list(FilenameFilter filter) • Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.
Methods of the File Class • File[] listFiles() • Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname. • File[] listFiles(FileFilter filter) • Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter. • File[] listFiles(FilenameFilter filter) • Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter. • static File[] listRoots() • List the available filesystem roots.
Methods of the File Class • boolean mkdir() • Creates the directory named by this abstract pathname. • boolean mkdirs() • Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. • boolean renameTo(File dest) • Renames the file denoted by this abstract pathname. • boolean setLastModified(long time) • Sets the last-modified time of the file or directory named by this abstract pathname. • boolean setReadOnly() • Marks the file or directory named by this abstract pathname so that only read operations are allowed.
Methods of the File Class • String toString() • Returns the pathname string of this abstract pathname. • URL toURL() • Converts this abstract pathname into a file: URL.
Byte Stream IO In the end all stream IO is a byte at a time.
Writing Byte Streams OutputStream FileOutputStream abstract void write(int b) void write(byte[] b) void write(byte[] b, int off, int len) void flush() void close() FileOutputStream(File file) FileOutputStream(String name) FileOutputStream(String name, boolean append) FileOutputStream(FileDescriptor fdObj)
import java.io.*; public class OutputAFile { public static void main(String[] args)throws IOException { byte[] b = "Goodbye\n".getBytes(); File aPath = new File("GoodbyeWorld.txt"); FileOutputStream fos = new FileOutputStream(aPath); for(int i = 0; i < b.length; i++) { fos.write(b[i]); } fos.close(); } } FileOutputStream Goodbye No Console Output
Reading Byte Streams InputStream FileInputStream abstract int read() int read(byte[] b) int read(byte[] b, int off, int len) int available() long skip(long n) void close() boolean markSupported() void mark(int readlimit) void reset() FileInputStream(File file) FileInputStream(String name) FileInputStream(FileDescriptor fdObj)
import java.io.*; public class InputAFile { public static void main(String[] args)throws IOException { File aPath = new File("HelloWorld.txt"); FileInputStream fis = new FileInputStream(aPath); while(fis.available() > 0) { System.out.print((char)fis.read()); } fis.close(); } } FileInputStream HelloWorld.txt Hi there world. Are you having a nice day? Hi there world. Are you having a nice day?
Writing Byte Streams OutputStream FilterOutputStream FileOutputStream BufferedOutputStream FilterOutputStream(OutputStream out) BufferedOutputStream(OutputStream out) BufferedOutputStream(OutputStream out, int size)
import java.io.*; public class OutputAFile { public static void main(String[] args)throws IOException { byte[] b = "Goodbye\n".getBytes(); File aPath = new File("GoodbyeWorld.txt"); FileOutputStream fos = new FileOutputStream(aPath); for(int i = 0; i < b.length; i++) { fos.write(b[i]); } fos.close(); } } import java.io.*; public class OutputAFile { public static void main(String[] args)throws IOException { byte[] b = "Goodbye\n".getBytes(); File aPath = new File("GoodbyeWorld.txt"); FileOutputStream fos = new FileOutputStream(aPath); BufferedOutputStream bos = new BufferedOutputStream(fos); for(int i = 0; i < b.length; i++) { bos.write(b[i]); } bos.close(); } } BufferedOutputStream Goodbye No Console Output
Reading Byte Streams InputStream FilterInputStream FileInputStream BufferedInputStream FilterInputStream(InputStream in) BufferedInputStream(InputStream in) BufferedInputStream(InputStream in, int size)
import java.io.*; public class InputAFile { public static void main(String[] args)throws IOException { File aPath = new File("HelloWorld.txt"); FileInputStream fis = new FileInputStream(aPath); while(fis.available() > 0) { System.out.print((char)fis.read()); } fis.close(); } } import java.io.*; public class InputAFile { public static void main(String[] args)throws IOException { File aPath = new File("HelloWorld.txt"); FileInputStream fis = new FileInputStream(aPath); BufferedInputStream bis = new BufferedInputStream(fis); while(bis.available() > 0) { System.out.print((char)bis.read()); } bis.close(); } } BufferedInputStream Hi there world. Are you having a nice day? Hi there world. Are you having a nice day?
Writing Byte Streams OutputStream FilterOutputStream FileOutputStream BufferedOutputStream DataOutputStream DataOutputStream(OutputStream out)
import java.io.*; public class OutputAFile { public static void main(String[] args)throws IOException { byte[] b = "Goodbye\n".getBytes(); File aPath = new File("file"); FileOutputStream fos = new FileOutputStream(aPath); BufferedOutputStream bos = new BufferedOutputStream(fos); for(int i = 0; i < b.length; i++) { bos.write(b[i]); } dos.close(); } } import java.io.*; public class OutputAFile { public static void main(String[] args)throws IOException { byte[] b = "Goodbye\n".getBytes(); File aPath = new File("file"); FileOutputStream fos = new FileOutputStream(aPath); BufferedOutputStream bos = new BufferedOutputStream(fos); DataOutputStream dos = new DataOutputStream(bos); for(int i = 0; i < b.length; i++) { dos.write(b[i]); } dos.close(); } } import java.io.*; public class OutputAFile { public static void main(String[] args)throws IOException { byte[] b = "Goodbye\n".getBytes(); File aPath = new File("file"); FileOutputStream fos = new FileOutputStream(aPath); BufferedOutputStream bos = new BufferedOutputStream(fos); DataOutputStream dos = new DataOutputStream(bos); dos.writeInt(b.length); for(int i = 0; i < b.length; i++) { dos.write(b[i]); } dos.close(); } } import java.io.*; public class OutputAFile { public static void main(String[] args)throws IOException { byte[] b = "Goodbye\n".getBytes(); File aPath = new File("file"); FileOutputStream fos = new FileOutputStream(aPath); BufferedOutputStream bos = new BufferedOutputStream(fos); DataOutputStream dos = new DataOutputStream(bos); dos.writeInt(b.length); for(int i = 0; i < b.length; i++) { dos.write(b[i]); } dos.writeFloat(3.14159f); dos.close(); } } import java.io.*; public class OutputAFile { public static void main(String[] args)throws IOException { byte[] b = "Goodbye\n".getBytes(); File aPath = new File("file"); FileOutputStream fos = new FileOutputStream(aPath); BufferedOutputStream bos = new BufferedOutputStream(fos); DataOutputStream dos = new DataOutputStream(bos); dos.writeInt(b.length); for(int i = 0; i < b.length; i++) { dos.write(b[i]); } dos.writeFloat(3.14159f); dos.writeUTF("Drive Carefully"); dos.close(); } } DataOutputStream Goodbye @IDriveCarefully No Console Output
import java.io.*; public class OutputAFile { public static void main(String[] args)throws IOException { byte[] b = "Goodbye\n".getBytes(); File aPath = new File("file"); FileOutputStream fos = new FileOutputStream(aPath); BufferedOutputStream bos = new BufferedOutputStream(fos); DataOutputStream dos = new DataOutputStream(bos); dos.writeInt(b.length); for(int i = 0; i < b.length; i++) { dos.write(b[i]); } dos.writeFloat(3.14159f); dos.writeUTF("Drive Carefully"); dos.close(); } } import java.io.*; public class OutputAFile { public static void main(String[] args)throws IOException { byte[] b = "Goodbye\n".getBytes(); File aPath = new File("file"); FileOutputStream fos = new FileOutputStream(aPath); BufferedOutputStream bos = new BufferedOutputStream(fos); DataOutputStream dos = new DataOutputStream(bos); dos.writeInt(b.length); for(int i = 0; i < b.length; i++) { dos.write(b[i]); } dos.writeFloat(3.14159f); dos.writeUTF("Drive Carefully"); dos.close(); } } import java.io.*; public class OutputAFile { public static void main(String[] args)throws IOException { byte[] b = "Goodbye\n".getBytes(); File aPath = new File("file"); FileOutputStream fos = new FileOutputStream(aPath); DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(fos)); dos.writeInt(b.length); for(int i = 0; i < b.length; i++) { dos.write(b[i]); } dos.writeFloat(3.14159f); dos.writeUTF("Drive Carefully"); dos.close(); } } import java.io.*; public class OutputAFile { public static void main(String[] args)throws IOException { byte[] b = "Goodbye\n".getBytes(); File aPath = new File("file"); FileOutputStream fos = new FileOutputStream(aPath); DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(fos)); dos.writeInt(b.length); for(int i = 0; i < b.length; i++) { dos.write(b[i]); } dos.writeFloat(3.14159f); dos.writeUTF("Drive Carefully"); dos.close(); } } import java.io.*; public class OutputAFile { public static void main(String[] args)throws IOException { byte[] b = "Goodbye\n".getBytes(); File aPath = new File("file"); FileOutputStream fos = new FileOutputStream(aPath); DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(fos)); dos.writeInt(b.length); for(int i = 0; i < b.length; i++) { dos.write(b[i]); } dos.writeFloat(3.14159f); dos.writeUTF("Drive Carefully"); dos.close(); } } import java.io.*; public class OutputAFile { public static void main(String[] args)throws IOException { byte[] b = "Goodbye\n".getBytes(); File aPath = new File("file"); DataOutputStream dos = new DataOutputStream( new BufferedOutputStream( new FileOutputStream(aPath))); dos.writeInt(b.length); for(int i = 0; i < b.length; i++) { dos.write(b[i]); } dos.writeFloat(3.14159f); dos.writeUTF("Drive Carefully"); dos.close(); } } import java.io.*; public class OutputAFile { public static void main(String[] args)throws IOException { byte[] b = "Goodbye\n".getBytes(); File aPath = new File("file"); DataOutputStream dos = new DataOutputStream( new BufferedOutputStream( new FileOutputStream(aPath))); dos.writeInt(b.length); for(int i = 0; i < b.length; i++) { dos.write(b[i]); } dos.writeFloat(3.14159f); dos.writeUTF("Drive Carefully"); dos.close(); } } About the Format
Reading Byte Streams InputStream FilterInputStream FileInputStream BufferedInputStream DataInputStream DataInputStream(InputStream in)
import java.io.*; public class InputAFile { public static void main(String[] args)throws IOException { File aPath = new File("file"); FileInputStream fis = new FileInputStream(aPath); BufferedInputStream bis = new BufferedInputStream(fis); while(bis.available() > 0) { System.out.print((char)bis.read()); } dis.close(); } } import java.io.*; public class InputAFile { public static void main(String[] args)throws IOException { File aPath = new File("file"); FileInputStream fis = new FileInputStream(aPath); BufferedInputStream bis = new BufferedInputStream(fis); DataInputStream dis = new DataInputStream(bis); while(bis.available() > 0) { System.out.print((char)dis.read()); } dis.close(); } } import java.io.*; public class InputAFile { public static void main(String[] args)throws IOException { File aPath = new File("file"); FileInputStream fis = new FileInputStream(aPath); BufferedInputStream bis = new BufferedInputStream(fis); DataInputStream dis = new DataInputStream(bis); int len = dis.readInt(); System.out.println("len = " + len); for(int i = 0; i < len; i++) { System.out.print((char)dis.read()); } dis.close(); } } import java.io.*; public class InputAFile { public static void main(String[] args)throws IOException { File aPath = new File("file"); FileInputStream fis = new FileInputStream(aPath); BufferedInputStream bis = new BufferedInputStream(fis); DataInputStream dis = new DataInputStream(bis); int len = dis.readInt(); System.out.println("len = " + len); for(int i = 0; i < len; i++) { System.out.print((char)dis.read()); } System.out.println(); dis.close(); } } import java.io.*; public class InputAFile { public static void main(String[] args)throws IOException { File aPath = new File("file"); FileInputStream fis = new FileInputStream(aPath); BufferedInputStream bis = new BufferedInputStream(fis); DataInputStream dis = new DataInputStream(bis); int len = dis.readInt(); System.out.println("len = " + len); for(int i = 0; i < len; i++) { System.out.print((char)dis.read()); } System.out.println(); System.out.println("dis.readFloat() = " + dis.readFloat()); dis.close(); } } import java.io.*; public class InputAFile { public static void main(String[] args)throws IOException { File aPath = new File("file"); FileInputStream fis = new FileInputStream(aPath); BufferedInputStream bis = new BufferedInputStream(fis); DataInputStream dis = new DataInputStream(bis); int len = dis.readInt(); System.out.println("len = " + len); for(int i = 0; i < len; i++) { System.out.print((char)dis.read()); } System.out.println(); System.out.println("dis.readFloat() = " + dis.readFloat()); System.out.println("dis.readUTF() = " + dis.readUTF()); dis.close(); } } import java.io.*; public class InputAFile { public static void main(String[] args)throws IOException { File aPath = new File("file"); FileInputStream fis = new FileInputStream(aPath); BufferedInputStream bis = new BufferedInputStream(fis); DataInputStream dis = new DataInputStream(bis); int len = dis.readInt(); System.out.println("len = " + len); for(int i = 0; i < len; i++) { System.out.print((char)dis.read()); } System.out.println(); System.out.println("dis.readFloat() = " + dis.readFloat()); System.out.println("dis.readUTF() = " + dis.readUTF()); dis.close(); } } DataInputStream len = 8 Goodbye dis.readFloat() = 3.14159 dis.readUTF() = Drive Carefully
Writing Byte Streams OutputStream FilterOutputStream FileOutputStream BufferedOutputStream DataOutputStream CheckedOutputStream DataOutputStream(OutputStream out, Checksum cksum)C
import java.io.*; import java.util.zip.*; public class OutputAFile { public static void main(String[] args)throws IOException { byte[] b = "Goodbye\n".getBytes(); File aPath = new File("file"); FileOutputStream fos = new FileOutputStream(aPath); BufferedOutputStream bos = new BufferedOutputStream(fos); DataOutputStream dos = new DataOutputStream(bos); dos.writeInt(b.length); for(int i = 0; i < b.length; i++) { dos.write(b[i]); } dos.writeFloat(3.14159f); dos.writeUTF("Drive Carefully"); dos.flush(); dos.close(); } } import java.io.*; import java.util.zip.*; public class OutputAFile { public static void main(String[] args)throws IOException { byte[] b = "Goodbye\n".getBytes(); File aPath = new File("file"); FileOutputStream fos = new FileOutputStream(aPath); BufferedOutputStream bos = new BufferedOutputStream(fos); CheckedOutputStream cos = new CheckedOutputStream(bos, new CRC32()); DataOutputStream dos = new DataOutputStream(cos); dos.writeInt(b.length); for(int i = 0; i < b.length; i++) { dos.write(b[i]); } dos.writeFloat(3.14159f); dos.writeUTF("Drive Carefully"); dos.flush(); dos.close(); } } import java.io.*; import java.util.zip.*; public class OutputAFile { public static void main(String[] args)throws IOException { byte[] b = "Goodbye\n".getBytes(); File aPath = new File("file"); FileOutputStream fos = new FileOutputStream(aPath); BufferedOutputStream bos = new BufferedOutputStream(fos); CheckedOutputStream cos = new CheckedOutputStream(bos, new CRC32()); DataOutputStream dos = new DataOutputStream(cos); dos.writeInt(b.length); for(int i = 0; i < b.length; i++) { dos.write(b[i]); } dos.writeFloat(3.14159f); dos.writeUTF("Drive Carefully"); dos.flush(); System.out.println(cos.getChecksum().getValue()); dos.close(); } } import java.io.*; import java.util.zip.*; public class OutputAFile { public static void main(String[] args)throws IOException { byte[] b = "Goodbye\n".getBytes(); File aPath = new File("file"); FileOutputStream fos = new FileOutputStream(aPath); BufferedOutputStream bos = new BufferedOutputStream(fos); CheckedOutputStream cos = new CheckedOutputStream(bos, new CRC32()); DataOutputStream dos = new DataOutputStream(cos); dos.writeInt(b.length); for(int i = 0; i < b.length; i++) { dos.write(b[i]); } dos.writeFloat(3.14159f); dos.writeUTF("Drive Carefully"); dos.flush(); System.out.println(cos.getChecksum().getValue()); dos.close(); } } CheckedOutputStream 92402406
Reading Byte Streams InputStream FilterInputStream FileInputStream BufferedInputStream DataInputStream CheckedInputStream DataInputStream(InputStream in, Checksum cksum)
import java.io.*; import java.util.zip.*; public class InputAFile { public static void main(String[] args)throws IOException { File aPath = new File("file"); FileInputStream fis = new FileInputStream(aPath); BufferedInputStream bis = new BufferedInputStream(fis); DataInputStream dis = new DataInputStream(bis); int len = dis.readInt(); System.out.println("len = " + len); for(int i = 0; i < len; i++) { System.out.print((char)dis.readByte()); } System.out.println("dis.readFloat() = " + dis.readFloat()); System.out.println("dis.readUTF() = " + dis.readUTF()); dis.close(); } } import java.io.*; import java.util.zip.*; public class InputAFile { public static void main(String[] args)throws IOException { File aPath = new File("file"); FileInputStream fis = new FileInputStream(aPath); BufferedInputStream bis = new BufferedInputStream(fis); CheckedInputStream cis = new CheckedInputStream(bis, new CRC32()); DataInputStream dis = new DataInputStream(cis); int len = dis.readInt(); System.out.println("len = " + len); for(int i = 0; i < len; i++) { System.out.print((char)dis.readByte()); } System.out.println("dis.readFloat() = " + dis.readFloat()); System.out.println("dis.readUTF() = " + dis.readUTF()); System.out.println("CRC32 = "+cis.getChecksum().getValue()); dis.close(); } } CheckedInputStream
import java.io.*; import java.util.zip.*; public class InputAFile { public static void main(String[] args)throws IOException { File aPath = new File("file"); FileInputStream fis = new FileInputStream(aPath); BufferedInputStream bis = new BufferedInputStream(fis); CheckedInputStream cis = new CheckedInputStream(bis, new CRC32()); DataInputStream dis = new DataInputStream(cis); int len = dis.readInt(); System.out.println("len = " + len); for(int i = 0; i < len; i++) { System.out.print((char)dis.readByte()); } System.out.println("dis.readFloat() = " + dis.readFloat()); System.out.println("dis.readUTF() = " + dis.readUTF()); System.out.println("CRC32 = "+cis.getChecksum().getValue()); dis.close(); } } CheckedInputStream len = 8 Goodbye dis.readFloat() = 3.14159 dis.readUTF() = Drive Carefully CRC32 = 92402406
Writing Byte Streams OutputStream FilterOutputStream FileOutputStream BufferedOutputStream DataOutputStream DigestOutputStream CheckedOutputStream DigestInputStream(InputStream in, Digest digest)
import java.io.*; import java.security.*; public class OutputAFile { public static void main(String[] args)throws IOException, NoSuchAlgorithmException { byte[] b = "Goodbye\n".getBytes(); File aPath = new File("file"); FileOutputStream fos = new FileOutputStream(aPath); BufferedOutputStream bos = new BufferedOutputStream(fos); DigestOutputStream digOS = new DigestOutputStream(bos, MessageDigest.getInstance("MD5")); DataOutputStream dos = new DataOutputStream(digOS); dos.writeInt(b.length); for(int i = 0; i < b.length; i++) { dos.write(b[i]); } dos.writeFloat(3.14159f); dos.writeUTF("Drive Carefully"); byte[] dig = digOS.getMessageDigest().digest(); for(int i = 0; i < dig.length; i++){ System.out.println(dig[i]); } dos.close(); } } DigestOutputStream
import java.io.*; import java.security.*; public class OutputAFile { public static void main(String[] args)throws IOException, NoSuchAlgorithmException { byte[] b = "Goodbye\n".getBytes(); File aPath = new File("file"); FileOutputStream fos = new FileOutputStream(aPath); BufferedOutputStream bos = new BufferedOutputStream(fos); DigestOutputStream digOS = new DigestOutputStream(bos, MessageDigest.getInstance("MD5")); DataOutputStream dos = new DataOutputStream(digOS); dos.writeInt(b.length); for(int i = 0; i < b.length; i++) { dos.write(b[i]); } dos.writeFloat(3.14159f); dos.writeUTF("Drive Carefully"); byte[] dig = digOS.getMessageDigest().digest(); for(int i = 0; i < dig.length; i++){ System.out.println(dig[i]); } dos.close(); } } DigestOutputStream -93 92 56 126 97 -78 67 -66 -36 -47 17 -16 -95 127 106 -2