0

I really do not want to do a duplicate question, but none of the answers on SO were implementable in my problem.

The answer in this question:

How to read a file from a certain offset in Java?

uses RandomAccessFile, but the implementations I found need all the file lines to have the same length.

How can I get List lines = readLinesFromLine(file);?

I tried

private static List<String> readRandomAccessFile(String filepath, int lineStart, int lineEnd, int charsPerLine, String delimiter) {

    File file = new File(filepath);

    String data = "";
   

    int bytesPerLine = charsPerLine+2;

    try{

        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");

        for (int i = lineStart; i <lineEnd ; i++) {

            randomAccessFile.seek(bytesPerLine *i);
            data = randomAccessFile.readLine();
            dialogLineRead.add(data);
        }

        randomAccessFile.close();

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

    String returnData = "";

    for (int i = 0; i < dialogLineRead.size(); i++) {

        returnData += dialogLineRead.get(i);
        returnData+=delimiter;
    }

    return returnData;

But like I said charsPerLine has to be the same for each line.

I tried to count the chars of each line in a file, and store it in a list, but with a log file of 2gb, that takes to much ram.

Any ideas?

OhleC
  • 2,821
  • 16
  • 29
kryzystof
  • 190
  • 2
  • 13

1 Answers1

0

For a standard text file where you don't know the line lengths in advance, there's really no way around reading the whole thing line by line, like in this answer, for example.

OhleC
  • 2,821
  • 16
  • 29