Ejemplo n.º 1
0
  /**
   * Load and Attach a DataSet from a File. The method loads the data into a DataSet class and
   * attaches the class to the graph for plotting.
   *
   * <p>The data is assumed to consist (at this stage) 2 ASCII columns of numbers x, y. As always
   * blank lines are ignored and everything following # is ignored as a comment.
   *
   * @param file The URL of the data file to read.
   * @return The DataSet constructed containing the data read.
   */
  public DataSet loadFile(URL file) {
    byte b[] = new byte[50];
    int nbytes = 0;
    int max = 100;
    int inc = 100;
    int n = 0;
    double data[] = new double[max];
    InputStream is = null;
    boolean comment = false;
    int c;

    try {
      is = file.openStream();

      while ((c = is.read()) > -1) {

        switch (c) {
          case '#':
            comment = true;
            break;
          case '\r':
          case '\n':
            comment = false;
          case ' ':
          case '\t':
            if (nbytes > 0) {
              String s = new String(b, 0, 0, nbytes);
              data[n] = Double.valueOf(s).doubleValue();
              n++;
              if (n >= max) {
                max += inc;
                double d[] = new double[max];
                System.arraycopy(data, 0, d, 0, n);
                data = d;
              }

              nbytes = 0;
            }
            break;
          default:
            if (!comment) {
              b[nbytes] = (byte) c;
              nbytes++;
            }
            break;
        }
      }

      if (is != null) is.close();
    } catch (Exception e) {
      System.out.println("Failed to load Data set from file ");
      e.printStackTrace();
      if (is != null)
        try {
          is.close();
        } catch (Exception ev) {
        }
      return null;
    }

    return loadDataSet(data, n / 2);
  }