Reading and Writing Data Code Samples
Often a program needs to read information in or write information out to a file, disk, the network, memory, or another program. The Java programming language provides a number of APIs for those purposes.
- ArrayClient.java creates two instances of SerializedObject and writes them to an output stream (the server). For supporting information see Transporting Objects over Sockets.
- ArrayMultiplier.java receives two objects, unpacks them, multiplies the arrays, and sends the output as an object to ArrayClient.java. For supporting information see Transporting Objects over Sockets.
- CheckedInputStream.java computes a checksum on data as it is read from a stream. For supporting information see The Java Tutorial: I/O: Reading and Writing.
- CheckedOutputStream.java computes a checksum on data written to a stream. For supporting information see The Java Tutorial: I/O: Reading and Writing.
- Checksum.java is an interface that defines four methods for checksum objects to implement to reset, update, and return the checksum value. For supporting information see The Java Tutorial: I/O: Reading and Writing.
- Concatenate.java concatenates files in the order in which they are received. For supporting information see The Java Tutorial: I/O: Reading and Writing.
- Copy.java copies the text contents of a file to another file. For supporting information see The Java Tutorial: I/O: Reading and Writing.
- CopyBytes.java copies the byte contents of a file to another file. For supporting information see The Java Tutorial: I/O: Reading and Writing.
- DateClient.java is a client program that receives a Date object from DateServer.java. For supporting information see Transporting Objects over Sockets.
- DateServer.java is multithreaded and listens for client requests and sends a Date object to the client. For supporting information see Transporting Objects over Sockets.
- FileReaderWriterExample.java shows you how to read data from and write data to a file. For supporting information see New to Java Programming Supplement, December 2001.
- ItemsListing.java shows how to convert an object to its string form. For supporting information see New to Java Programming Supplement, August 2001.
- MyClass.java uses an array of ObjectStreamField objects to explicitly serialize specific fields. For supporting information see Advanced Object Serialization.
- NonBlockingReadURL.java reads the main page of a web site through an HTTP request with error handling added. For supporting information see New I/O Funcitonality for Java 2 Standard Edition 1.4.
- ReadDate.java reads a serialized object, restores its state, and prints the information. For supporting information see Transporting Objects over Sockets.
- ReadFile.java displays information about a File object whose name is passed in from the command line. If the file exists and is a normal file, its contents are displayed. For supporting information see New to Java Programming Supplement, Feb 2002.
- ReadInfo.java has a readObject method that can be called multiple times to read objects from an input stream. For supporting information see Transporting Objects over Sockets.
- ReadURL.java reads the main page of a web site through an HTTP request. For supporting information see New I/O Funcitonality for Java 2 Standard Edition 1.4.
- ReverseThread.java writes its output to a pipe writer and returns a pipe reader to the caller. For supporting information see The Java Tutorial: I/O: Reading and Writing.
- RhymingWords.java connects the input and output of its own methods to create a list of rhyming words. For supporting information see The Java Tutorial: I/O: Reading and Writing.
- SaveDate.java saves a Date object to a file. For supporting information see Transporting Objects over Sockets.
- SaveInfo.java has a writeObject method that can be called multiple times to write objects to the output stream. For supporting information see Transporting Objects over Sockets.
- SerializedObject.java implements the Serializable interface. For supporting information see Transporting Objects over Sockets.
- SortThread.java reads and sorts data. For supporting information see The Java Tutorial: I/O: Reading and Writing.
- Server.java is a single-threaded server, sending back a canned text message for each request. For supporting information see New I/O Funcitionality for Java 2 Standard Edition 1.4.
- TestBufferedReader.javareads a line of text and returns it as a string. For supporting information see New to Java Programming Supplement, Feb 2002.
- URLConnection.java and URLConnection.java (abstract class) make up a custom URL connection to resolve a URL into a resource stream. For supporting information see A New Era for Java Protocol Handlers.
- UserInfo.java is a custom class that implements the Serializable interface. For supporting information see Transporting Objects over Sockets.
- ValidationExample.java reads a serialized object and validates its data to ensure it has not been tampered with. For supporting information see Advanced ObjectSerialization.