Use RandomAccessFile to save an object : RandomAccessFile « File Input Output « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » File Input Output » RandomAccessFileScreenshots 
Use RandomAccessFile to save an object
  

import java.io.IOException;
import java.io.RandomAccessFile;

public class CreateEmployeeFile {
  public static void main(String[] argsthrows Exception {
    String[] fnames = "A""B""C" };

    String[] lnames = "a""b""c" };

    String[] addresses = "Box 100""55 Street""6 Lane" };

    byte[] ages = 465932 };

    double[] salaries = 5.06.03.0 };

    RandomAccessFile raf = new RandomAccessFile("employee.dat""rw");

    EmployeeRecord er = new EmployeeRecord();

    for (int i = 0; i < fnames.length; i++) {
      er.setFirstName(fnames[i]);
      er.setLastName(lnames[i]);
      er.setAddress(addresses[i]);
      er.setAge(ages[i]);
      er.setSalary(salaries[i]);
      er.write(raf);
    }
    raf = new RandomAccessFile("employee.dat""rw");

    er = new EmployeeRecord();

    int numRecords = (intraf.length() / er.size();

    for (int i = 0; i < numRecords; i++) {
      er.read(raf);

      System.out.print(er.getFirstName() " ");
      System.out.print(er.getLastName() " ");
      System.out.print(er.getAddress() " ");
      System.out.print(er.getAge() " ");
      System.out.println(er.getSalary());
    }
    raf.seek(0);
    for (int i = 0; i < numRecords; i++) {
      er.read(raf);
      if (er.getAge() >= 55) {
        er.setSalary(0.0);
        raf.seek(raf.getFilePointer() - er.size());
        er.write(raf);
        raf.seek(raf.getFilePointer() - er.size());
        er.read(raf);
      }
      System.out.print(er.getFirstName() " ");
      System.out.print(er.getLastName() " ");
      System.out.print(er.getAddress() " ");
      System.out.print(er.getAge() " ");
      System.out.println(er.getSalary());
    }

  }
}

class EmployeeRecord {
  private String lastName;

  private String firstName;

  private String address;

  private byte age;

  private double salary;

  void read(RandomAccessFile rafthrows IOException {
    char[] temp = new char[15];
    for (int i = 0; i < temp.length; i++)
      temp[i= raf.readChar();
    lastName = new String(temp);
    temp = new char[15];
    for (int i = 0; i < temp.length; i++)
      temp[i= raf.readChar();
    firstName = new String(temp);
    temp = new char[30];
    for (int i = 0; i < temp.length; i++)
      temp[i= raf.readChar();

    address = new String(temp);
    age = raf.readByte();
    salary = raf.readDouble();
  }

  void write(RandomAccessFile rafthrows IOException {
    StringBuffer sb;
    if (lastName != null)
      sb = new StringBuffer(lastName);
    else
      sb = new StringBuffer();

    sb.setLength(15);
    raf.writeChars(sb.toString());

    if (firstName != null)
      sb = new StringBuffer(firstName);
    else
      sb = new StringBuffer();

    sb.setLength(15);
    raf.writeChars(sb.toString());

    if (address != null)
      sb = new StringBuffer(address);
    else
      sb = new StringBuffer();

    sb.setLength(30);
    raf.writeChars(sb.toString());
    raf.writeByte(age);
    raf.writeDouble(salary);
  }

  void setAge(byte age) {
    this.age = age;
  }

  byte getAge() {
    return age;
  }

  void setAddress(String address) {
    this.address = address;
  }

  String getAddress() {
    return address;
  }

  void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  String getFirstName() {
    return firstName;
  }

  void setLastName(String lastName) {
    this.lastName = lastName;
  }

  String getLastName() {
    return lastName;
  }

  void setSalary(double salary) {
    this.salary = salary;
  }

  double getSalary() {
    return salary;
  }

  int size() {
    return (15 15 309;
  }
}

   
    
  
Related examples in the same category
1.Random IO
2.Using the RandomAccessFile class
3.The RandomAccessFile Class
4.Use RandomAccessFile to reverse a file
5.Using a Random Access File
6.A RandomAccessFile object that is tied to a file called employee.dat.
7.Reading UTF-8 Encoded Data
8.Use RandomAccessFile class
9.Appending data to existing file
10.The class demonstrates the use of java.io.RandomAccessFile
11.Readonly RandomAccessFile
12.Translate Charset
13.Reverse a file with RandomAccessFile
14.Read from back
15.Using RandomAccessFile to read file saved DataOutputStreamUsing RandomAccessFile to read file saved DataOutputStream
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.