O'Reilly logo

Java RMI by William Grosso

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Viewing a File

To make this discussion more concrete, we will now discuss a simple application that allows the user to display the contents of a file in a JTextArea. The application is called ViewFile and is shown in Example 1-1. Note that the application’s main( ) method is defined in the com.ora.rmibook.chapter1.ViewFile class.[5] The resulting screenshot is shown in Figure 1-1.

The ViewFile application

Figure 1-1. The ViewFile application

Example 1-1. ViewFile.java

public class ViewfileFrame extends ExitingFrame{
//  lots of code to set up the user interface.
//  The View button's action listener is an inner class

	private void copyStreamToViewingArea(InputStream fileInputStream)
         throws IOException {
		BufferedInputStream bufferedStream = new BufferedInputStream(fileInputStream);
		int nextByte;
		StringBuffer localBuffer = new StringBuffer(  );
		while( -1 != (nextByte = bufferedStream.read(  )))   {
			char nextChar = (char) nextByte; 	
		_fileViewingArea.append(localBuffer.toString(  ));

	private class ViewFileAction extends AbstractAction {
		public ViewFileAction(  ) {
			putValue(Action.NAME, "View");
			putValue(Action.SHORT_DESCRIPTION, "View file contents in main text area.");

		public void actionPerformed(ActionEvent event) {
			FileInputStream fileInputStream = _fileTextField.getFileInputStream(  );
			if (null==fileInputStream) {
				_fileViewingArea.setText("Invalid file name");
			else {
				try {
					 fileInputStream.close(  );
				 catch (java.io.IOException ioException)  {
					_fileViewingArea.setText("\n Error occured while reading file");

The important part of the code is the View button’s action listener and the copyStreamToViewingArea( ) method. copyStreamToViewingArea( ) takes an instance of InputStream and copies the contents of the stream to the central JTextArea. What happens when a user clicks on the View button? Assuming all goes well, and that no exceptions are thrown, the following three lines of code from the buttons’s action listener are executed:

FileInputStream fileInputStream = _fileTextField.getFileInputStream(  );
fileInputStream.close(  );

The first line is a call to the getFileInputStream( ) method on _fileTextField. That is, the program reads the name of the file from a text field and tries to open a FileInputStream. FileInputStream is defined in the java.io* package. It is a subclass of InputStream used to read the contents of a file.

Once this stream is opened, copyStreamToViewingArea( ) is called. copyStream-ToViewingArea( ) takes the input stream, wraps it in a buffer, and then reads it one byte at a time. There are two things to note here:

  • We explicitly check that nextByte is not equal to -1 (e.g., that we’re not at the end of the file). If we don’t do this, the loop will never terminate, and we will we will continue to append (char) -1 to the end of our text until the program crashes or throws an exception.

  • We use BufferedInputStream instead of using FileInputStream directly. Internally, a BufferedInputStream maintains a buffer so it can read and store many values at one time. Maintaining this buffer allows instances of Buffered-InputStream to optimize expensive read operations. In particular, rather than reading each byte individually, bufferedStream converts individual calls to its read( ) method into a single call to FileInputStream’s read(byte[] buffer) method. Note that buffering also provides another benefit. BufferedInputStream supports stream navigation through the use of marking.


Of course, the operating system is probably already buffering file reads and writes. But, as we noted above, even the act of passing data to the operating system (which uses native methods) is expensive and ought to be buffered.

[5] This example uses classes from the Java Swing libraries. If you would like more information on Swing, see Java Swing (O’Reilly) or Java Foundation Classes in a Nutshell (O’Reilly).

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required