Search code examples
javastringbufferrandomaccessfile

Write and Read file with RandomAccessFile in Java


Hello I am writing a program that write and read file XML in Java. Here is the Writing file

public static void main(String[] args) throws IOException {
    File file = new File("C:\\Test\\employee.XML");
    RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
    //int codeEmp = 0;
    String nameEmp[] = {"Name A", "Name B", "Name C", "Name D", "Name E"};
    String addEmp[] = {"Address A", "Address B",
            "Address C", " Address D",
            "Address E"};
    int saleEmp[] = {2000,1232,7653,1236,3452};
    int comEmp[] = {400,100,3000,300,500};

    StringBuffer buffer;
    StringBuffer buffer1;


    for (int i=0;i< nameEmp.length; i++){
        randomAccessFile.writeInt(i+1);

        buffer = new StringBuffer( nameEmp[i]);
        buffer.setLength(10);
        randomAccessFile.writeChars(buffer.toString());

        buffer1 = new StringBuffer( addEmp[i]);
        buffer1.setLength(100);
        randomAccessFile.writeChars(buffer1.toString());


        randomAccessFile.writeInt(saleEmp[i]);
        randomAccessFile.writeInt(comEmp[i]);
    }
    randomAccessFile.close();
}

The Reader is

public static void main(String[] args) throws IOException {
    File file = new File("C:\\Test\\employee.XML");
    RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");

    int codeEmp, position = 0;
    char nameEmp[] = new char[10];
    char addEmp[] = new char [100];
    int saleEmp , comEmp;

    for(;;){
        randomAccessFile.seek(position);
        codeEmp = randomAccessFile.readInt();

        for (int i = 0; i < nameEmp.length; i++) {
            nameEmp[i] = randomAccessFile.readChar();
        }
        String nameEmpS= new String(nameEmp);

        for (int i = 0; i < addEmp.length; i++) {
            addEmp[i] = randomAccessFile.readChar();
        }
        String addEmpS= new String(addEmp);

        saleEmp =randomAccessFile.readInt();
        comEmp=randomAccessFile.readInt();

        System.out.println("Cod Emp: " + codeEmp + ", nombre: " +nameEmpS+ ", dirección: "+addEmpS+
                ", sale: " + saleEmp+ ", comisión: " + comEmp );

        position= position + 36;

        if (randomAccessFile.getFilePointer()==file.length())break;
    }
    randomAccessFile.close();
}

The problem is that when I run the reader file, it's return many lines and only the first line is okay but the rest are wrong. How can I fix it? Here is the console

Cod Emp: 1, nombre: Name A , dirección: Address A , sale: 2000, comisión: 400

Cod Emp: 7536672, nombre: A , dirección: Name B Ad, sale: 6553714, comisión: 6619251

Cod Emp: 0, nombre: , dirección: Ɛ Name B Address B , sale: 0, comisión: 0


Solution

  • You are assigning a "random" next read position here:

    position= position + 36;
    

    That makes no sense unless there is 36 bytes padding after every record. Comment out the line position= position + 36; and randomAccessFile.seek(position); because if you have the matched up each write with a read then the next seek position is moved correctly.

    Also note:

    • writing a file called employee.XML which isn't XML format is very misleading for others.
    • You don't need to use RandomAccessFile here as all your writes are sequential.