Exemplo n.º 1
0
  /**
   * Appends a string to the file and returns the offset of the start of the string.
   *
   * @param str the string to be written.
   * @return the offset of the start of the string that was written.
   * @throws IOException if an I/O error occurs or the offset would be larger than 2^32-1.
   */
  private long writeString(String str) throws IOException {
    // Write the string at the end of the file.
    long pos = stringFile.length();
    if (pos > MASK32) throw new IOException("String file too large");

    stringFile.seek(pos);
    stringFile.writeUTF(str);
    return pos;
  }
Exemplo n.º 2
0
  /**
   * Writes the Item Code to the Items Random Access File
   *
   * @param file - the Items Random Access File
   */
  private void writeCodeToFile(RandomAccessFile file) {
    String code = getCode();
    while (code.length() != 6) {
      code = "0" + code;
    }

    try {
      file.writeUTF(code);
    } catch (Exception e) {

    }
  }
Exemplo n.º 3
0
Arquivo: RAF.java Projeto: 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
Exemplo n.º 4
0
  /**
   * Writes the username of the user that created the Item to the Items Random Access File
   *
   * @param file- the Item's Random Access File
   */
  private void writeCreatedBy(RandomAccessFile file) {
    String createdBy = getCreatedBy();

    try {
      while (createdBy.length() != 20) {
        createdBy = createdBy + " ";
      }

      file.writeUTF(createdBy);
    } catch (Exception e) {

    }
  }
Exemplo n.º 5
0
  /**
   * Writes the Item Name to the Items Random Access File
   *
   * @param file - the Items Random Access File
   */
  private void writeNameToFile(RandomAccessFile file) {
    String name = getName();

    while (name.length() != 200) {
      name = name + " ";
    }

    try {
      file.writeUTF(name);
    } catch (Exception e) {

    }
  }
Exemplo n.º 6
0
  /**
   * Writes the Item description to the Items Random Access File
   *
   * @param file - the Items Random Access File
   */
  private void writeDescriptionToFile(RandomAccessFile file) {
    String description = getDescription();

    if (description == null || description.equals("")) { // As description is optional
      description = "<No description>";
    }

    while (description.length() != 200) {
      description = description + " ";
    }

    try {
      file.writeUTF(description);
    } catch (Exception e) {

    }
  }
  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) {
      }
    }
  }
Exemplo n.º 8
0
  public boolean writeObj(Object obj, String fileName) {
    RandomAccessFile file = null;

    if (!this.configDirExists(fileName)) {
      return false;
    }

    try {
      file = new RandomAccessFile(fileName, "rw");
      String xml = xstream.toXML(obj);
      file.writeUTF(xml);
    } catch (IOException ex) {
      ex.printStackTrace();
      return false;
    } finally {
      try {
        file.close();
      } catch (IOException ex) {
        Logger.getLogger(ConfigLoader.class.getName()).log(Level.SEVERE, null, ex);
      }
    }

    return true;
  }