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.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");
     lab1 = new Label("Complete path of the selected file");
     ta1 = new TextArea(40, 20);
     add(openPlease, "South");
     add(ta1, "Center");
     add(lab1, "North");

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

Java AWT FileDialog Open Save

Output screen of Java AWT FileDialog Open Save

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

Leave a Comment

Your email address will not be published.