user1808288
user1808288

Reputation: 1

Read a file by Random access file line by line and get part of strings from each line

My goal is to read line in the text file and seek a position in file and then read the string up to the given length from the position by using randomaccessfile IO in java. I have written the following code and my code is going to infinite loop and printing the first line always. below is the code. Can some one help me? package test;

import java.io.RandomAccessFile;
import java.util.LinkedHashMap;
import java.util.Map;

public class MyTest {

    static byte[] b;
    static Map<String, Integer> pos = new LinkedHashMap<>();
    static Map<String, Integer> length = new LinkedHashMap<>();
    static RandomAccessFile raf;
    static String str = null;

    public static void main(String[] args) throws Exception {
        pos.put("name", 0);
        pos.put("age", 3);
        pos.put("gender", 8);

        length.put("name", 2);
        length.put("age", 6);
        length.put("gender", 10);
        raf = new RandomAccessFile("mytext", "r");
        while ((str = raf.readLine()) != null) {
            for (Map.Entry<String, Integer> map : pos.entrySet()) {
                String key = map.getKey();
                read("mytext", map.getValue(), length.get(key));
            }
        }
    }

    public static void read(String path, int position, int length) throws Exception {
        b = new byte[500];
        raf.seek(position);
        raf.read(b, position, length);
        System.out.println(new String(b));
    }
}

Upvotes: 0

Views: 2901

Answers (2)

Pedro Molina
Pedro Molina

Reputation: 1474

In this example, we first write data in an file with ramdom access file, and after we read it.

public static void main(String[] args) throws IOException {
        int[] listaNumeros = new int[20];
        ArrayList<Integer> num1 = new ArrayList();
        for (int i = 0; i < 20 ; i++) {
            listaNumeros[i]=numAleatorios(20,40);
        }
        try(RandomAccessFile doc = new RandomAccessFile("ficheros/numeros2.txt","rw");){
            for(int n:listaNumeros)
                doc.writeInt(n);
            //System.out.println(doc.length());
            //String s = String.valueOf(doc.read());
            //System.out.println(s);
            //num1.add(doc.read());

        }catch (IOException e){
            e.printStackTrace();
        }

        try(RandomAccessFile doc = new RandomAccessFile("ficheros/numeros2.txt","rw");){
            while(doc.getFilePointer()<doc.length()){
                System.out.println(doc.readInt());
                num1.add(doc.readInt());
            }
            System.out.println(media(num1));
        }catch (IOException e){
            e.printStackTrace();
        }
    }

Dont forget using different RamdomAccessFil writing and reading.

Upvotes: 0

sbstnzmr
sbstnzmr

Reputation: 421

The documentation specifies readLine() as

Reads the next line of text from this file. This method successively reads bytes from the file, starting at the current file pointer, until it reaches a line terminator or the end of the file. [...]

and seek(long pos) as

Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs. [...]

So in your read() you set the pointer to another position within your readLine() loop. If that position is again the beginning of the file, you will just read the first line over and over again.

Upvotes: 1

Related Questions