/** * Exports to a file. * * @param outputFileName The output file name * @throws InvalidFileFormatException If the file name doesn't end in .mid or .midi */ public void exportToFile(String outputFileName) throws InvalidFileFormatException { // Check for a valid file format. if (!outputFileName.endsWith(".mid") && !outputFileName.endsWith(".midi")) { String msg = "File names must end in .mid or .midi"; throw new InvalidFileFormatException(msg); } // Find a supported file type, and export the file. int[] types = MidiSystem.getMidiFileTypes(sequence); try { File outputFile = new File(outputFileName); MidiSystem.write(sequence, types[0], outputFile); } catch (IOException ex) { ex.printStackTrace(); System.exit(1); } }
public boolean saveMidiFile(File file) { try { int[] fileTypes = MidiSystem.getMidiFileTypes(sequence); if (fileTypes.length == 0) { System.out.println("Can't save sequence"); return false; } else { if (MidiSystem.write(sequence, fileTypes[0], file) == -1) { throw new IOException("Problems writing to file"); } return true; } } catch (SecurityException ex) { ex.printStackTrace(); return false; } catch (Exception ex) { ex.printStackTrace(); return false; } }