How to mark a line in a file?

I have a text file. It is designed as follows:

# 1 {1,12,345,867}
# 2 {123, 3243534, 2132131231} 
# 3 {234, 35345}
#4{}

... (at the end of each entry is "\ n")

That's an example. Actually my lines #number {number, number, ..., number} can be very long ...

Here is the class constructor template that works with this file:

public Submatrix(String matrixFilePath, int startPos, int endPos) throws FileNotFoundException{

}

As you can see, the submatrix is ​​determined by the startPos and endPos numbers of the matrix rows.

My question is: "How could I count the rows to achieve the right one?" My file can contain billions of lines. Should I use LineNumberReader-> readLine () billions of times ?????

+3
source share
4 answers

, , , readLine, , ,

  • , GregS, .. -
  • , , , -

    0000001 000000001024
    0000002 000000001064
    0000003 000000002010
    

    , :

    3, 3, (3-1) * 20, 0000003 000000002010, , 3 2010, .

    , , , , . , , .

EDIT

Python script, . .

script 06d, 999.999, ( INDEX_LENGTH). ( java :)

script :

python create_index.py data.txt data.idx 3

:

#1{1,12,345,867}
#2{123, 3243534, 2132131231}
#3{234, 35345}
#4{}

script:

import sys

# Usage: python this_script.py datafile indexfile lineno
# indexfile will be overwritten
# lineno is the data line which will be printed using the
# index file, as a demonstration
datafilename= sys.argv[1]
indexfilename = sys.argv[2]
lineno = int(sys.argv[3])

# max 999999 lines in this format
format = "%06d\n"
INDEX_LENGTH = 6+1 # +1 for newline


def create_indexfile():
        indexfile = open(indexfilename, "wB")
        # Print index of first line
        indexfile.write(format % 0)

        f = open(datafilename, "rB")
        line = f.readline()
        while len(line) > 0:
                indexfile.write( format % f.tell() )
                line = f.readline()
        f.close()
        indexfile.close()

# Retrieve the data of 1 line in the data file
# using the index file
def get_line():
        linepos = INDEX_LENGTH * (lineno - 1)

        indexfile = open(indexfilename, "rB")
        indexfile.seek(linepos)
        datapos = int(indexfile.readline())
        indexfile.close()

        datafile = open(datafilename, "rB")
        datafile.seek(datapos)
        print datafile.readline()
        datafile.close()


if __name__ == '__main__':
        create_indexfile()
        get_line()

. , , (№ 3 {...}) , .

, , .

+2

, . , , . , , . N , ..

+5

@extraneon

, #number {number, number,...}

package logic;

public class DenominatedBinaryRow{
private int sn;
private BinaryRow row;

public DenominatedBinaryRow(int sn, BinaryRow row){
    this.sn = sn;
    this.row = row;
}

public DenominatedBinaryRow plus(int sn, DenominatedBinaryRow addend){
    return new DenominatedBinaryRow(sn, this.row.plus(addend.row));
}

public int getSn(){
    return this.sn;
}

public BinaryRow getRow(){
    return this.row;
}

public boolean equals(Object obj){
    DenominatedBinaryRow res = (DenominatedBinaryRow) obj;
    if (this.getSn() == res.getSn() && this.getRow().equals(res.getRow())){
        return true;
    }
    return false;
}

 }

, , , BinaryRow ( ) ? , ( )? (, )

package logic;

import java.util.*;

public class BinaryRow {
private List<Integer> row;

public BinaryRow(){
    this.row = new ArrayList<Integer>();
}

public List<Integer> getRow(){
    return this.row;
}

public void add(Integer arg){
    this.getRow().add(arg);
}

public Integer get(int index){
    return this.getRow().get(index);
}

public int size(){
    return this.getRow().size();
}


public BinaryRow plus(BinaryRow addend){
    BinaryRow result = new BinaryRow();

    //suppose, rows are already sorted (ascending order)
    int i = this.size();
    int j = addend.size();
    while (i > 0 && j > 0)
        if (this.get(this.size() - i) < addend.get(addend.size() - j)){
            result.add(this.get(this.size() - i));
            i--;
        }
        else if (this.get(this.size() - i) > addend.get(addend.size() - j)){
            result.add(addend.get(addend.size() - j));
            j--;
        }
        else{
            result.add(this.get(this.size() - i));
            i--;
            j--;
        }

    if (i > 0){
        for (int k = this.size() - i; k < this.size(); k++)
            result.add(this.get(k));
    }
    if (j > 0){
        for (int k = addend.size() - j; k < addend.size(); k++)
            result.add(addend.get(k));
    }

    return result;
}

public boolean equals(Object obj){
    BinaryRow binRow = (BinaryRow) obj;
    if (this.size() == binRow.size()){
        for (int i = 0; i < this.size(); i++){
            if (this.getRow().get(i) != binRow.getRow().get(i)) return false;
        }
        return true;
    }
    return false;
}

public long convertToDec(){
    long result = 0;
    for (Integer next : this.getRow()) {
        result += Math.pow(2, next);
    }

    return result;
}

}

+1

I'm afraid that you need to get to the xth line, you will need to call readLine () x times. This means reading all the data until you reach this line. Each character can be the end of a line, so there is no way to the xth line without reading each character before this line.

-1
source

Source: https://habr.com/ru/post/1734682/


All Articles