简体   繁体   English

通过随机访问文件逐行读取文件并从每行获取部分字符串

[英]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.我的目标是读取文本文件中的行并在文件中寻找一个位置,然后通过在 java 中使用 randomaccessfile IO 从该位置读取到给定长度的字符串。 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));
    }
}

The documentation specifies readLine() as文档将readLine()指定为

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) asseek(long pos)作为

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.因此,在您的read()您将指针设置为readLine()循环中的另一个位置。 If that position is again the beginning of the file, you will just read the first line over and over again.如果该位置再次成为文件的开头,您将一遍又一遍地阅读第一行。

In this example, we first write data in an file with ramdom access file, and after we read it.在这个例子中,我们首先将数据写入具有 ramdom 访问文件的文件中,然后我们读取它。

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.不要忘记使用不同的 RamdomAccessFil 写入和读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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