Way2Java

Java AWT FileDialog Open Save

The FileDialog class provides a File Open or Save dialog box enabling us to access the local file system. The FileDialog class of Java is OS dependent and calls operating system's specific Open File dialog box or Save dialog box. The advantage of this is that the users of Java program will see familiar screens.

Following is the class signature

public class FileDialog extends Dialog

Example on FileDialog

In the following file dialog program, when the user clicks the button, the Open dialog box of the Windows OS is opened. The file selected by the user is read and opened in a text area. At the same time, the complete path of the selected file is displayed.

 
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class FDTest extends Frame implements ActionListener  
{
   FileDialog fd1;
   Button openPlease;
   Label lab1;
   TextArea ta1;
   public FDTest()  
   {
     fd1 = new FileDialog(this, "Select File to Open");
     openPlease = new Button("Open File");
     openPlease.setBackground(Color.pink);
     lab1 = new Label("Complete path of the selected file");
     ta1 = new TextArea(40, 20);
			
     add(openPlease, "South");
     add(ta1, "Center");
     add(lab1, "North");
     openPlease.addActionListener(this);

     setTitle("FileDialog Practice");  
     setSize(525, 325);  
     setVisible(true);
				               // a shortcut to close the frame
     addWindowListener(new WindowAdapter()   
     {
          public void windowClosing(WindowEvent e)   
          {
            System.exit(0);	
          }
     });
   }
   public void actionPerformed(ActionEvent e)  
   {
     fd1.setVisible(true);
     lab1.setText("Directory: " + fd1.getDirectory());  
     display(fd1.getDirectory() + fd1.getFile());
   } 
   public void display(String fname)  
   {                                           // this method is for reading a file
     try  
     {
       FileInputStream fis1 = new FileInputStream(fname);
       int fileSize = fis1.available();
       byte buf1[] = new byte[fileSize];
       fis1.read(buf1);
       String str1 = new String(buf1);
       ta1.setText(str1);
     }
     catch(IOException e) 
     { 
       System.exit(0); 
     } 
   }
   public static void main(String args[])   
   {  
     new FDTest();  
   }
}

Output screen of 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).