[an error occurred while processing this directive]

Open The Default Editor to Edit the File

                         

This section, you will see the method of opening the file in the default editor to edit and save the file. Here an example has been provided with the complete Java code for illustrating all the things that helps to open the file in the default editor to edit the file.

When you run the application this will need the file name with the complete path where it exists in your system. And then the application will open the file in the default editor.

Code Description:

Here as you can see the Desktop class is used for launching the file in the default editor to edit. This class allows Java Application to launch associated applications registered on the native desktop to handle a URI or a file. It's edit method opens the file in the default editor to edit.

Here is the code of the program:

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

public class OpenDefaultEditorToEditFile {
  public static void main(String[] a) {
    String filename = "";
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the file name with complete path: ");
    try {
      filename = in.readLine();
      File file = new File(filename);
      if(!file.exists() && file.length() 0){
        System.out.println("Specified file does not exist!");
        System.exit(0);
      }
      Desktop desktop = null;
      if (Desktop.isDesktopSupported()) {
        desktop = Desktop.getDesktop();
      }
      desktop.edit(file);
    }
    catch (IOException ioe) {
      ioe.printStackTrace();
    }
  }
}

Download the Example

                         

[an error occurred while processing this directive]