Random File : Files « File Input Output « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » File Input Output » FilesScreenshots 
Random File
Random File


import java.io.DataInput;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomFileTest {
  public static void main(String[] args) {
    Employee[] staff = new Employee[3];

    staff[0new Employee("Harry Hacker"35000);
    staff[1new Employee("Carl Cracker"75000);
    staff[2new Employee("Tony Tester"38000);
    try {
      DataOutputStream out = new DataOutputStream(new FileOutputStream(
          "employee.dat"));
      for (int i = 0; i < staff.length; i++)
        staff[i].writeData(out);
      out.close();
    catch (IOException e) {
      System.out.print("Error: " + e);
      System.exit(1);
    }

    try {
      RandomAccessFile in = new RandomAccessFile("employee.dat""r");
      int count = (int) (in.length() / Employee.RECORD_SIZE);
      Employee[] newStaff = new Employee[count];

      for (int i = count - 1; i >= 0; i--) {
        newStaff[inew Employee();
        in.seek(i * Employee.RECORD_SIZE);
        newStaff[i].readData(in);
      }
      for (int i = 0; i < newStaff.length; i++)
        newStaff[i].print();
    catch (IOException e) {
      System.out.print("Error: " + e);
      System.exit(1);
    }
  }
}

class Employee {
  public static final int NAME_SIZE = 40;

  public static final int RECORD_SIZE = * NAME_SIZE + 4;

  private String name;

  private double salary;

  public Employee(String n, double s) {
    name = n;
    salary = s;
  }

  public Employee() {
  }

  public void print() {
    System.out.println(name + " " + salary);
  }
  public void writeData(DataOutput outthrows IOException {
    DataIO.writeFixedString(name, NAME_SIZE, out);
    out.writeDouble(salary);
  }

  public void readData(DataInput inthrows IOException {
    name = DataIO.readFixedString(NAME_SIZE, in);
    salary = in.readDouble();
  }
}

class DataIO {
  public static String readFixedString(int size, DataInput dataInput)
      throws IOException {
    StringBuffer strBuffer = new StringBuffer(size);
    int count = 0;
    boolean more = true;
    while (more && count < size) {
      char aChar = dataInput.readChar();
      count++;
      if (aChar == 0)
        more = false;
      else
        strBuffer.append(aChar);
    }
    dataInput.skipBytes((size - count));
    return strBuffer.toString();
  }

  public static void writeFixedString(String str, int size, DataOutput dataOutput)
      throws IOException {

    for (int i = 0; i < size; i++) {
      char aChar = 0;
      if (i < str.length())
        aChar = str.charAt(i);
      dataOutput.writeChar(aChar);
    }
  }
}
           
       
Related examples in the same category
1. Find out the directoryFind out the directory
2. Listing the Directory ContentsListing the Directory Contents
3. Data fileData file
4. Output to a text File
5. Choose a FileChoose a File
6. Read data from text file
7. Copy File
8. Querying a File for Information
9. Working with RandomAccessFileWorking with RandomAccessFile
10. Get a list of files, and check if any files are missing
11. Delete a file from within Java
12. Work with temporary files in Java
13. Compare File Dates
14. Strings -- extract printable strings from binary file
15. File Name Bean
w___w___w_.___j_a___v_a_2s.___c_o__m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.