View Source

h3.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|http://java.sun.com/developer/technicalArticles/ALT/sockets/#9/] creates two instances of {{SerializedObject}} and writes them to an output stream (the server). For supporting information see [Transporting Objects over Sockets|http://java.sun.com/developer/technicalArticles/ALT/sockets/].

* [ArrayMultiplier.java|http://java.sun.com/developer/technicalArticles/ALT/sockets/#10/] 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|http://java.sun.com/developer/technicalArticles/ALT/sockets/].

* [CheckedInputStream.java|http://java.sun.com/docs/books/tutorial/essential/io/examples/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|http://java.sun.com/docs/books/tutorial/essential/io/].

* [CheckedOutputStream.java|http://java.sun.com/docs/books/tutorial/essential/io/examples/CheckedOutputStream.java] computes a checksum on data written to a stream. For supporting information see [The Java Tutorial: I/O: Reading and Writing|http://java.sun.com/docs/books/tutorial/essential/io/].

* [Checksum.java|http://java.sun.com/docs/books/tutorial/essential/io/examples/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|http://java.sun.com/docs/books/tutorial/essential/io/].

* Concatenate.java concatenates files in the order in which they are received. For supporting information see [The Java Tutorial: I/O: Reading and Writing|http://java.sun.com/docs/books/tutorial/essential/io/].

* [Copy.java|http://java.sun.com/docs/books/tutorial/essential/io/examples/Copy.java] copies the text contents of a file to another file. For supporting information see [The Java Tutorial: I/O: Reading and Writing|http://java.sun.com/docs/books/tutorial/essential/io/].

* [CopyBytes.java|http://java.sun.com/docs/books/tutorial/essential/io/examples/CopyBytes.java] copies the byte contents of a file to another file. For supporting information see [The Java Tutorial: I/O: Reading and Writing|http://java.sun.com/docs/books/tutorial/essential/io/].

* [DateClient.java|http://java.sun.com/developer/technicalArticles/ALT/sockets/#7/] is a client program that receives a Date object from {{DateServer.java}}. For supporting information see [Transporting Objects over Sockets|http://java.sun.com/developer/technicalArticles/ALT/sockets/].

* [DateServer.java|http://java.sun.com/developer/technicalArticles/ALT/sockets/#6/] is multithreaded and listens for client requests and sends a Date object to the client. For supporting information see [Transporting Objects over Sockets|http://java.sun.com/developer/technicalArticles/ALT/sockets/].

* [FileReaderWriterExample.java|http://java.sun.com/new2java/supplements/2001/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|http://java.sun.com/new2java/supplements/2001/dec01.html].

* [ItemsListing.java|http://java.sun.com/new2java/supplements/2001/ItemsListing.java] shows how to convert an object to its string form. For supporting information see [New to Java Programming Supplement, August 2001|http://java.sun.com/new2java/supplements/2001/aug01.html].

* [MyClass.java|http://java.sun.com/developer/technicalArticles/ALT/serialization/#2/] uses an array of {{ObjectStreamField}} objects to explicitly serialize specific fields. For supporting information see [Advanced Object Serialization|http://java.sun.com/developer/technicalArticles/ALT/serialization/].

* [NonBlockingReadURL.java|http://java.sun.com/developer/technicalArticles/releases/nio/#code2] 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|http://java.sun.com/developer/technicalArticles/releases/nio/].

* [ReadDate.java|http://java.sun.com/developer/technicalArticles/ALT/sockets/#2/] reads a serialized object, restores its state, and prints the information. For supporting information see [Transporting Objects over Sockets|http://java.sun.com/developer/technicalArticles/ALT/sockets/].

* [ReadFile.java|http://java.sun.com/new2java/supplements/2002/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|http://java.sun.com/new2java/supplements/2002/feb02.html].

* [ReadInfo.java|http://java.sun.com/developer/technicalArticles/ALT/sockets/#5/] 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|http://java.sun.com/developer/technicalArticles/ALT/sockets/].

* [ReadURL.java|http://java.sun.com/developer/technicalArticles/releases/nio/#code1] 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|http://java.sun.com/developer/technicalArticles/releases/nio/].

* [ReverseThread.java|http://java.sun.com/docs/books/tutorial/essential/io/examples/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|http://java.sun.com/docs/books/tutorial/essential/io/].

* [RhymingWords.java|http://java.sun.com/docs/books/tutorial/essential/io/examples/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|http://java.sun.com/docs/books/tutorial/essential/io/].

* [SaveDate.java|http://java.sun.com/developer/technicalArticles/ALT/sockets/#1/] saves a {{Date}} object to a file. For supporting information see [Transporting Objects over Sockets|http://java.sun.com/developer/technicalArticles/ALT/sockets/].

* [SaveInfo.java|http://java.sun.com/developer/technicalArticles/ALT/sockets/#4/] 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|http://java.sun.com/developer/technicalArticles/ALT/sockets/].

* [SerializedObject.java|http://java.sun.com/developer/technicalArticles/ALT/sockets/#8/] implements the {{Serializable}} interface. For supporting information see [Transporting Objects over Sockets|http://java.sun.com/developer/technicalArticles/ALT/sockets/].

* [SortThread.java|http://java.sun.com/docs/books/tutorial/essential/io/examples/SortThread.java] reads and sorts data. For supporting information see [The Java Tutorial: I/O: Reading and Writing|http://java.sun.com/docs/books/tutorial/essential/io/].

* [Server.java|http://java.sun.com/developer/technicalArticles/releases/nio/#code3] 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|http://java.sun.com/developer/technicalArticles/releases/nio/].

* [TestBufferedReader.java|http://java.sun.com/new2java/supplements/2002/TestBufferedReader.java]reads a line of text and returns it as a string. For supporting information see [New to Java Programming Supplement, Feb 2002|http://java.sun.com/new2java/supplements/2002/feb02.html].

* [URLConnection.java|http://java.sun.com/developer/onlineTraining/protocolhandlers/#code1] and [URLConnection.java (abstract class)|http://java.sun.com/developer/onlineTraining/protocolhandlers/#code2] 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|http://java.sun.com/developer/onlineTraining/protocolhandlers/].

* [UserInfo.java|http://java.sun.com/developer/technicalArticles/ALT/sockets/#3/] is a custom class that implements the {{Serializable}} interface. For supporting information see [Transporting Objects over Sockets|http://java.sun.com/developer/technicalArticles/ALT/sockets/].

* [ValidationExample.java|http://java.sun.com/developer/technicalArticles/ALT/serialization/#1/] reads a serialized object and validates its data to ensure it has not been tampered with. For supporting information see [Advanced ObjectSerialization|http://java.sun.com/developer/technicalArticles/ALT/serialization/].
----

The individuals who post here are part of the extended Sun Microsystems community and they might not be employed or in any way formally affiliated with Sun Microsystems. The opinions expressed here are their own, are not necessarily reviewed in advance by anyone but the individual authors, and neither Sun nor any other party necessarily agrees with them.

Copyright 1994-2009 Sun Microsystems, Inc.
Powered by Atlassian Confluence
Sun Guidelines on Public Discourse Privacy Policy Terms of Use Trademarks Site Map Employment Investor Relations Contact