import java.awt.*;
import java.io.*;

        // Allow the contents of a file to be viewed within
        // a TextArea. The area has both vertical and horizontal
        // scrollbars, and is made non-editable.
public class DisplayFile extends Frame {
    public DisplayFile(){
        final int width = 400, height = 300;
        setSize(width,height);

        // Create the viewing area for the file's contents.
        final int numRows = 10, numCols = 80;
        TextArea area = new TextArea("",numRows,numCols,
                                     TextArea.SCROLLBARS_BOTH);
        area.setBackground(Color.white);

        area.setFont(new Font("Helvetica",Font.PLAIN,10));
        // Don't allow changes.
        area.setEditable(false);
        add(area);
        setContents(area);
    }

        // Display the contents of the given file in the viewing area.
    public void display(String filename) throws Exception {
        // Make sure the file exists.
        File details = new File(filename);
        if(!details.exists()){
            throw new Exception(filename+" does not exist.");
        }
        else if(!details.canRead()){
            throw new Exception("You don't have permission to view: "+filename);
        }
        else if(details.length() > Integer.MAX_VALUE){
            // We can't cope with the file's size.
            throw new Exception("The file is too big to display.");
        }
        else{
            loadFile(details);
        }
    }

        // Load the contents of the given file into the TextArea.
        // The existence and readability of the file should have
        // already been ascertained, but failure must still be
        // anticipated.
    protected void loadFile(File details) throws Exception {
        final String filename = details.getName();
        BufferedReader reader = null;
        try{
            // Read the file into a single array.
            int fileSize = (int) details.length();
            char[] buffer = new char [fileSize];
            // Keep track of how much has been read.
            int totalCharsRead = 0;

            // Open the file and read it.
            reader = new BufferedReader(new FileReader(filename));
            while(totalCharsRead < fileSize){
                // Try to read the remainder in one go.
                int charsRead = reader.read(buffer,totalCharsRead,
                                            fileSize-totalCharsRead);
                totalCharsRead += charsRead;
            }

            // Place the text into the TextArea.
            TextArea area = getContents();
            area.setText(String.valueOf(buffer));
        }
        catch(IOException e){
            throw new Exception("Read failure: "+e.getMessage());
        }
        finally{
            if(reader != null){
                reader.close();
            }
        }
    }

    protected TextArea getContents(){
        return contents;
    }

    protected void setContents(TextArea c){
        contents = c;
    }

    private TextArea contents;
}
