简体   繁体   中英

Read/Write from RandomAccessFile in Java

I am trying my hands on using RandomAccessFile. What I'm doing is trying to store messages in the file and try to get all messages from one pointer to another

//file is the RandomAccessFile
//newLine is just new line

How I'm Storing:

//I want to store a message per line
public void store(long idx, String message){

   file.seek(idx);
   file.write(message);
   file.write(newLine)
}

This is how I fill the file:

void createDummy(String location) throws IOException {

    Random r = new Random();

    for (int i = 0; i < 10; i++) {

        long l = i;
        StringBuffer b = new StringBuffer();
        for (int j = 0; j < 10; j++) {

            b.append((char)r.nextInt(26)+65);
        }

        String str = b.toString();
        store(l, str);
        System.out.println("Stored "+str);
    }

}

This was the output:

Stored BYOENDRWEBUVGIFR
Stored HNXRKZWPAZUYCUHN
Stored RPRRAXWRBFSRSHJD
Stored AGTJXZMCFXENBQOH
Stored QLWXLIBKRPELYHPR
Stored VBUOVQICIOOTOTYK
Stored HPDZRPUGIYAFBGJE
Stored ZGLAIXTBGVCLJAGC
Stored UZKUXCWGCIVJLMAF
Stored RIXZSEDXQPAVDWEK

Reading...how I'm reading:

//I want to retrieve all messages from "from" to "to"

public void retrieve(long from, long to){

 long start = from;
 file.seek(start);

 while(start < to){ 
    System.out.println(file.readLine());
    start++;
    file.seek(start);
  }
}

Calling retrieve(3,9); gives the below output// Retrieve from 3 to 9;

Read NRPRAGTJQLWXLVBUOVQHPDZRPUZGLAIXTBUZKUXCWGCRIXZSEDXQPAVDWEKBT                    WNWFVVYDPEMCSFVQ                                RXCZSNOBEPPDKNBB                                            LCPFDVUXWETHVWJQ                                                        CPCCDTSOCLSEXIST                                                                    QZFCQWMQVEXTOGNG                                                                                SSKTXOYMOBETGVWN                                                                                            DWYIMILEXQMSNUBJ
Read RPRAGTJQLWXLVBUOVQHPDZRPUZGLAIXTBUZKUXCWGCRIXZSEDXQPAVDWEKBT                    WNWFVVYDPEMCSFVQ                                RXCZSNOBEPPDKNBB                                            LCPFDVUXWETHVWJQ                                                        CPCCDTSOCLSEXIST                                                                    QZFCQWMQVEXTOGNG                                                                                SSKTXOYMOBETGVWN                                                                                            DWYIMILEXQMSNUBJ
Read PRAGTJQLWXLVBUOVQHPDZRPUZGLAIXTBUZKUXCWGCRIXZSEDXQPAVDWEKBT                    WNWFVVYDPEMCSFVQ                                RXCZSNOBEPPDKNBB                                            LCPFDVUXWETHVWJQ                                                        CPCCDTSOCLSEXIST                                                                    QZFCQWMQVEXTOGNG                                                                                SSKTXOYMOBETGVWN                                                                                            DWYIMILEXQMSNUBJ
Read RAGTJQLWXLVBUOVQHPDZRPUZGLAIXTBUZKUXCWGCRIXZSEDXQPAVDWEKBT                    WNWFVVYDPEMCSFVQ                                RXCZSNOBEPPDKNBB                                            LCPFDVUXWETHVWJQ                                                        CPCCDTSOCLSEXIST                                                                    QZFCQWMQVEXTOGNG                                                                                SSKTXOYMOBETGVWN                                                                                            DWYIMILEXQMSNUBJ
Read AGTJQLWXLVBUOVQHPDZRPUZGLAIXTBUZKUXCWGCRIXZSEDXQPAVDWEKBT                    WNWFVVYDPEMCSFVQ                                RXCZSNOBEPPDKNBB                                            LCPFDVUXWETHVWJQ                                                        CPCCDTSOCLSEXIST                                                                    QZFCQWMQVEXTOGNG                                                                                SSKTXOYMOBETGVWN                                                                                            DWYIMILEXQMSNUBJ
Read GTJQLWXLVBUOVQHPDZRPUZGLAIXTBUZKUXCWGCRIXZSEDXQPAVDWEKBT                    WNWFVVYDPEMCSFVQ                                RXCZSNOBEPPDKNBB                                            LCPFDVUXWETHVWJQ                                                        CPCCDTSOCLSEXIST                                                                    QZFCQWMQVEXTOGNG                                                                                SSKTXOYMOBETGVWN                                                                                            DWYIMILEXQMSNUBJ

I expected to retrieve the below but's that's not the case:

Read AGTJXZMCFXENBQOH Read QLWXLIBKRPELYHPR Read VBUOVQICIOOTOTYK Read HPDZRPUGIYAFBGJE Read ZGLAIXTBGVCLJAGC Read UZKUXCWGCIVJLMAF

Please what's wrong with my retrieve() function. Any feedback is appreciated. Thank you.

Two things :

  1. What is your newLine exactly, it should be "\\n".

  2. When you read that file in your while loop, you do not need to call

    file.seek(start);

    at all. Cause the cursor will automatically goes to the next line after

    file.readLine();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM