Beispiel #1
0
 /** {@inheritDoc} */
 public float readFloat() throws IOException {
   fileSize -= 4;
   if (fileSize < 0) {
     throw new EOFException();
   }
   return dataInput.readFloat();
 }
Beispiel #2
0
    public boolean hasNext() {
      if (raf == null || !raf.getChannel().isOpen()) return false;

      if (flagNext == true) return true; // Déjà lue

      flagNext = true;
      try {
        next = new Entry();
        next.timestamp = (long) raf.readInt();
        next.value = raf.readFloat();
        if (this.end != null && next.timestamp > this.end) {
          next = null;
          close();
          return false;
        }
        return true;
      } catch (IOException e) {
        // EOF ou autre erreur d'IO
        if (!(e instanceof EOFException)) logger.log(Level.WARNING, e.getMessage(), e);
        next = null;
        try {
          close();
        } catch (IOException e1) {
          logger.log(Level.WARNING, e.getMessage(), e);
        }
        return false;
      }
    }
Beispiel #3
0
  public List<Entry> getLastPoints(int nb) throws IOException {
    File file = getFile();
    RandomAccessFile raf = null;
    List<Entry> result = null;

    try {
      raf = new RandomAccessFile(file, "r");
      long pos = raf.length() - (nb * DATA_LEN);
      if (pos < 0) {
        nb = nb + (int) (pos / DATA_LEN);
        pos = 0;
      }
      raf.seek(pos);

      result = new ArrayList<Entry>(nb);
      Entry next = null;
      for (int i = 0; i < nb; i++) {
        next = new Entry();
        next.timestamp = (long) raf.readInt();
        next.value = raf.readFloat();
        result.add(0, next);
      }
    } finally {
      if (raf != null) raf.close();
    }
    return result;
  }
Beispiel #4
0
  public static void main(String args[]) {
    try {
      String fname = "d:\\q.txt";
      String mode;
      // mode = "r";//r : file must exist
      mode = "rw"; // rw : file will be created or opened

      // open the file
      RandomAccessFile raf = new RandomAccessFile(fname, mode);

      /*
      //seek and write demo

         raf.seek(10);//position file r/w pointer at index 10 wrt BOF
         //a seek beyond file size causes file to grow upto the seek value
         raf.write(65);//raf.write('A');

      */

      // r/w java datatypes
      int i1, i2;
      float f1, f2;
      char c1, c2;
      String s1, s2;

      i1 = -10;
      f1 = 1234.5678F;
      c1 = 'q';
      s1 = "hello files";

      raf.seek(0); // reach BOF
      raf.writeInt(i1);
      raf.writeFloat(f1);
      raf.writeChar(c1);
      raf.writeUTF(s1);

      raf.seek(0); // reach BOF
      i2 = raf.readInt();
      f2 = raf.readFloat();
      c2 = raf.readChar();
      s2 = raf.readUTF();

      System.out.println(i2);
      System.out.println(f2);
      System.out.println(c2);
      System.out.println(s2);

      // close the file
      raf.close();
    } catch (IOException ex) {
      System.out.println(ex); // ex converts into ex.toString()
    }
  } // main
Beispiel #5
0
  public static void main(String args[]) {
    RandomAccessFile classe = null;
    Scanner entrada = new Scanner(System.in);
    Aluno b = new Aluno();
    boolean achou = false;
    float media;

    try {
      classe = new RandomAccessFile("c:\\alunos.dat", "rw");

      achou = false;
      while (true) {
        b.numero = classe.readInt();
        b.nome = classe.readUTF();
        b.curso = classe.readUTF();
        b.nota1 = classe.readFloat();
        b.nota2 = classe.readFloat();
        media = (float) ((b.nota1 + b.nota2) / 2);
        if (media >= 7) {
          System.out.print("\nNome: " + b.nome);
          System.out.print("\nCurso: " + b.curso);
          System.out.print("\nNota1: " + b.nota1);
          System.out.print("\nNota2: " + b.nota2);
          System.out.print("\nMedia: " + media + "\n");
        }
      }
    } // fim try
    catch (EOFException e) {
      try {
        classe.close();

      } // fim try
      catch (IOException e2) {
        System.out.println("\nErro de I/O");
      }
    } // fimn catch (EOFException e)
    catch (IOException e) {
      System.out.println("Erro na criacao o arquivo");
    }
  } // fim main
Beispiel #6
0
  /**
   * Fourni la dernière valeur Le curseur est positionné en fin de dernière valeur (pret à écrire si
   * ouvert en rw)
   *
   * @return
   * @throws IOException
   */
  private Entry getLast(RandomAccessFile rdf) throws IOException {
    if (this.last == null) {
      File file = getFile();
      long len = file.length();
      if (len >= DATA_LEN) {
        // Il y a des données dans le fichier : lecture
        boolean closeFileFlag = false;
        if (rdf == null) {
          rdf = new RandomAccessFile(file, "r");
          closeFileFlag = true;
        }
        long pos = len - DATA_LEN;
        long mod = pos % DATA_LEN;
        if (mod != 0) {
          logger.warning(
              "Taille de fichier incoherence ("
                  + len / DATA_LEN
                  + "x"
                  + DATA_LEN
                  + ", reste "
                  + mod
                  + "). retour a "
                  + (len - mod));
          pos -= mod;
        }
        rdf.seek(pos);

        last = new Entry();
        last.timestamp = (long) rdf.readInt();
        last.value = rdf.readFloat();
        logger.fine("dernier enregistrement: " + last);

        if (closeFileFlag) rdf.close();
      }
    }

    return this.last;
  }
Beispiel #7
0
 /** This reads a binary float from the file. */
 public float readBinaryFloat(int col, int row) throws IOException {
   raf.seek(row * nBytesPerRow + columnStartAt[col]);
   return raf.readFloat();
 }
 /* @see java.io.DataInput.readFloat() */
 public float readFloat() throws IOException {
   return raf.readFloat();
 }