Home Java Example Java Awt Open The Default Editor to Edit the File
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Open The Default Editor to Edit the File

Advertisement
This section, you will see the method of opening the file in the default editor to edit and save the file.

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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 13, 2009

Related Tutorials

Ask Questions?    Discuss: Open The Default Editor to Edit the File   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com