Java AWT FileDialog Open Save

fd1 = new FileDialog(this, "Select the File to Open");

In the above statement, "this" denotes the object of Frame class. The second string parameter becomes the title to the Open dialog box of the Windows OS.

A button openPlease is created to have the action that opens the Open dialog box. A label lab is created to display the path of the selected file. The text area ta is meant for to display the file contents.

getDirectory() and getFile() are the methods of FileDialog class that return the directory name and file name of the file user selects.

A helper method display() is used to read the file selected by the user. fis1.available() method returns the file size. A buf1[] array is created of length equivalent to the size of the file. read() method reads the file into the array. The data of the byte array is converted into a string and passed to the setText() method of text area. Now the file contents are displayed in the text area.

If an exception occurs in file reading, the program is closed with System.exit(0) in the catch block. The exit(0) method of System class terminates the execution of the running program (or to say, terminates the execution).

7 thoughts on “Java AWT FileDialog Open Save”

  1. how can i create a save as dialog box that can save a file with a given file name
    I request you sir to kindly write a program to create a save as dialog box
    please……….

Leave a Comment

Your email address will not be published.