Wednesday, March 2, 2011

How to open a file with associated program in Java

First you need following imports
  import java.awt.Desktop;
  import java.io.File;
  import java.io.IOException;

Now you can create a Desktop object and call the open() method.
  Desktop desktop = Desktop.getDesktop();
  File f = new File("C:\\readme.txt");
  try {
    desktop.open(f);
  } catch (IOException ex) {}

No comments:

Post a Comment