public static void openFile ( final File file ) throws IOException { // Check that the file is a file if ( ! file . isFile ( ) ) throw new IOException ( "Passed file is not a file." ) ; // Check that the Desktop API is supported if ( ! Desktop . isDesktopSupported ( ) ) { throw new IllegalStateException ( "Desktop is not supported" ) ; } final Desktop desktop = Desktop . getDesktop ( ) ; // Check that the open functionality is supported if ( ! desktop . isSupported ( Desktop . Action . OPEN ) ) { throw new IllegalStateException ( "Desktop doesn't support the open action" ) ; } // Open the file desktop . open ( file ) ; }
Opens a file using the OS default program for the file type using the Java Desktop API .