Sunday, May 6, 2012

JAVA Open a file using the default application editor.

// Example Foo.xls will be opened with Excel
// Make sure to import the class java.awt.Desktop

public static void OpenFile(String filename){
  try{
    // Check if class 'Desktop' is supported on the platform 
    if (Desktop.isDesktopSupported()) {
      Desktop.getDesktop().open(new File(filename));
    }else{
      System.out.println("Class Desktop is unsupported");
    }
  } catch (Exception ex){
    System.out.println("Error opening "+ filename);
  }
}

No comments:

Post a Comment