ui/src/main/java/ucar/nc2/ui/grib/Grib2DataPanel.java
private void writeToFile(List beans) {

    if (fileChooser == null)
      fileChooser = new FileManager(null, null, null, (PreferencesExt) prefs.node("FileManager"));

    FileOutputStream fos = null;
    RandomAccessFile raf = null;

    try {
      String filename = null;
      boolean append = false;
      int n = 0;
      MFile curr = null;

      for (Object o : beans) {
        Grib2RecordBean bean = (Grib2RecordBean) o;
        MFile mfile = fileList.get(bean.gr.getFile());
        if (curr == null || curr != mfile) {
          if (raf != null) raf.close();
          raf = new RandomAccessFile(mfile.getPath(), "r");
          curr = mfile;
        }

        if (fos == null) {
          String defloc = mfile.getPath();
          filename = fileChooser.chooseFilenameToSave(defloc + ".grib2");
          if (filename == null) return;
          File f = new File(filename);
          append = f.exists();
          fos = new FileOutputStream(filename, append);
        }

        Grib2SectionIndicator is = bean.gr.getIs();
        int size = (int) (is.getMessageLength());
        long startPos = is.getStartPos();
        if (startPos < 0) {
          JOptionPane.showMessageDialog(Grib2DataPanel.this, "Old index does not have message start - record not written");
        }

        byte[] rb = new byte[size];
        raf.seek(startPos);
        raf.readFully(rb);
        fos.write(rb);
        n++;
      }

      JOptionPane.showMessageDialog(Grib2DataPanel.this, filename + ": " + n + " records successfully written, append=" + append);

    } catch (Exception ex) {
      JOptionPane.showMessageDialog(Grib2DataPanel.this, "ERROR: " + ex.getMessage());
      ex.printStackTrace();

    } finally {
      try {
        if (fos != null) fos.close();
        if (raf != null) raf.close();
      } catch (IOException ioe) {
      }
    }
  }
private void writeToFile(List beans) {

    if (fileChooser == null)
      fileChooser = new FileManager(null, null, null, (PreferencesExt) prefs.node("FileManager"));

    FileOutputStream fos = null;
    RandomAccessFile raf = null;

    try {
      String filename = null;
      boolean append = false;
      int n = 0;
      MFile curr = null;

      for (Object o : beans) {
        Grib2RecordBean bean = (Grib2RecordBean) o;
        MFile mfile = fileList.get(bean.gr.getFile());
        if (curr == null || curr != mfile) {
          if (raf != null) raf.close();
          raf = new RandomAccessFile(mfile.getPath(), "r");
          curr = mfile;
        }

        if (fos == null) {
          String defloc = mfile.getPath();
          filename = fileChooser.chooseFilenameToSave(defloc + ".grib2");
          if (filename == null) return;
          File f = new File(filename);
          append = f.exists();
          fos = new FileOutputStream(filename, append);
        }

        Grib2SectionIndicator is = bean.gr.getIs();
        int size = (int) (is.getMessageLength());
        long startPos = is.getStartPos();
        if (startPos < 0) {
          JOptionPane.showMessageDialog(Grib2DataPanel.this, "Old index does not have message start - record not written");
        }

        byte[] rb = new byte[size];
        raf.seek(startPos);
        raf.readFully(rb);
        fos.write(rb);
        n++;
      }

      JOptionPane.showMessageDialog(Grib2DataPanel.this, filename + ": " + n + " records successfully written, append=" + append);

    } catch (Exception ex) {
      JOptionPane.showMessageDialog(Grib2DataPanel.this, "ERROR: " + ex.getMessage());
      ex.printStackTrace();

    } finally {
      try {
        if (fos != null) fos.close();
        if (raf != null) raf.close();
      } catch (IOException ioe) {
      }
    }
  }
private void writeToFile ( List beans ) { if ( fileChooser == null ) fileChooser = new FileManager ( null , null , null , ( PreferencesExt ) prefs . node ( "FileManager" ) ) ; FileOutputStream fos = null ; RandomAccessFile raf = null ; try { String filename = null ; boolean append = false ; int n = 0 ; MFile curr = null ; for ( Object o : beans ) { Grib2RecordBean bean = ( Grib2RecordBean ) o ; MFile mfile = fileList . get ( bean . gr . getFile ( ) ) ; if ( curr == null || curr != mfile ) { if ( raf != null ) raf . close ( ) ; raf = new RandomAccessFile ( mfile . getPath ( ) , "r" ) ; curr = mfile ; } if ( fos == null ) { String defloc = mfile . getPath ( ) ; filename = fileChooser . chooseFilenameToSave ( defloc + ".grib2" ) ; if ( filename == null ) return ; File f = new File ( filename ) ; append = f . exists ( ) ; fos = new FileOutputStream ( filename , append ) ; } Grib2SectionIndicator is = bean . gr . getIs ( ) ; int size = ( int ) ( is . getMessageLength ( ) ) ; long startPos = is . getStartPos ( ) ; if ( startPos < 0 ) { JOptionPane . showMessageDialog ( Grib2DataPanel . this , "Old index does not have message start - record not written" ) ; } byte [ ] rb = new byte [ size ] ; raf . seek ( startPos ) ; raf . readFully ( rb ) ; fos . write ( rb ) ; n ++ ; } JOptionPane . showMessageDialog ( Grib2DataPanel . this , filename + ": " + n + " records successfully written, append=" + append ) ; } catch ( Exception ex ) { JOptionPane . showMessageDialog ( Grib2DataPanel . this , "ERROR: " + ex . getMessage ( ) ) ; ex . printStackTrace ( ) ; } finally { try { if ( fos != null ) fos . close ( ) ; if ( raf != null ) raf . close ( ) ; } catch ( IOException ioe ) { } } }
////////////////////////////
////////////////////////////