【问题标题】:Read a file by Random access file line by line and get part of strings from each line通过随机访问文件逐行读取文件并从每一行获取部分字符串
【发布时间】:2019-01-26 13:28:58
【问题描述】:

我的目标是读取文本文件中的行并在文件中寻找一个位置,然后使用 java 中的 randomaccessfile IO 从该位置读取字符串到给定长度。我编写了以下代码,我的代码将无限循环并始终打印第一行。下面是代码。有人能帮我吗? 封装测试;

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));
    }
}

【问题讨论】:

    标签: java file-io


    【解决方案1】:

    documentation 指定 readLine()

    从此文件中读取下一行文本。此方法从文件中连续读取字节,从当前文件指针开始,直到到达行终止符或文件末尾。 [...]

    seek(long pos) 一样

    设置文件指针偏移量,从该文件的开头开始测量,下一次读取或写入发生在该位置。 [...]

    因此,在您的 read() 中,您将指针设置到 readLine() 循环中的另一个位置。如果该位置再次是文件的开头,您将一遍又一遍地阅读第一行。

    【讨论】:

    • 能否提供工作代码,以便我更好地理解这一点?
    • 我需要更好的解释你想做什么。您是要逐行阅读还是要寻找一个位置然后阅读一定的长度或组合。如果是组合,请准确说明您想要实现的目标。
    • 我有一个文件,想逐行读取。读取每一行后,我需要从不同位置获取该行中的值,并将这些值作为输入传递给不同的方法来处理它们。这是我的要求。如果您需要更多信息,请告诉我
    • 那么最好先阅读这行,不一定要使用 RFA,然后处理你得到的字符串。
    【解决方案2】:

    在这个例子中,我们首先将数据写入一个具有随机访问文件的文件中,然后读取它。

    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();
            }
        }
    

    不要忘记使用不同的 RamdomAccessFil 写入和读取。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-28
      • 1970-01-01
      • 2019-11-11
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      相关资源
      最近更新 更多