Ejemplo n.º 1
0
Archivo: RAF.java Proyecto: Utlesh/code
  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
Ejemplo n.º 2
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
  public My_Room_AC_db(String name) {
    y = new ArrayList<String>();

    try {
      raf = new RandomAccessFile("ac_db.dat", "r");
      String arr2[] = name.split("#");
      while (true) {
        String rec = raf.readUTF();
        String arr[] = rec.split("#");
        if (arr[0].equals(arr2[0])) {
          y.add(name);
        } else {
          y.add(rec);
        }
      }
    } catch (Exception ep) {
    } finally {
      try {
        // if(raf!=null)
        raf.close();
        File f = new File("ac_db.dat", "r");
        f.delete();
      } catch (Exception ex) {
      }
    }

    raf = null;
    try {
      raf = new RandomAccessFile("ac_db.dat", "rw");
      for (String ss : y) {
        raf.writeUTF(ss);
      }
    } catch (Exception ef) {
    } finally {
      try {
        raf.close();
      } catch (Exception ex) {
      }
    }
  }
Ejemplo n.º 4
0
  public T readObj(String fileName) {
    RandomAccessFile file = null;
    T retObj = null;

    if (!configDirExists(fileName)) {
      return null;
    }

    try {
      file = new RandomAccessFile(fileName, "rw");
      if (debug) {
        System.out.printf("ConfigLoader reading:%s\n", fileName);
      }
      retObj = (T) xstream.fromXML(file.readUTF());

    } catch (IOException ex) {
      System.err.printf(
          "Warning: failed to read config:%s : %s -- %s\n",
          configPathToFile, fileName, getPathToConfigDir());
      return null;
    } finally {
      try {
        if (file != null) {
          file.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    if (retObj != null && retObj instanceof ConfPortfolio) {
      ConfPortfolio config = (ConfPortfolio) retObj;
      if (config.useCurentDayAsEndingDate) {
        config.inputEndingDate = new DateTime();
      }
    }

    return retObj;
  }
Ejemplo n.º 5
0
 private String readString(long strOffset) throws IOException {
   stringFile.seek(strOffset);
   return stringFile.readUTF();
 }