2

I'm having a problem with my code, I would like to modify a line in the file when I'm reading it in the reverse order.

The code that I have is this:

try {
    ReversedLinesFileReader rf = new ReversedLinesFileReader(fileSql);

    while ((line = rf.readLine()) != null) {            
        if (Pattern.matches(".*"+pomVersion+".*", line)) {
            // HERE IS WHEN I WANT TO MODIFY THE THE LINE OF THE FILE WHILE I'M READING IT IN REVERSE ORDEN

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

So, I don't know how could I modify that line in specific while I'm reading the file in reserve order, any idea?

Thanks in advance!

Anish B.
  • 9,111
  • 3
  • 21
  • 41
  • There will be a method to modify a line in the ReversedLinesFileReader ? Have you searched it through the IDE ? – Anish B. Jul 05 '19 at 06:39
  • 1
    @AnishB. there is no such method, this class can only `readLine()` and `close()`. – deHaar Jul 05 '19 at 06:40
  • Is there any specific reason for you to read a file bottom-up? Can't you do it top-down? I think you have to store each line in a `List` (or similar) and re-write the file if you modify any line. – deHaar Jul 05 '19 at 06:41
  • 1
    @deHaar it's because performance, the file is so heavy and I need only the last 20 to 10 lines – Vasyl Havrylyuk Jul 05 '19 at 06:47
  • 1
    @AnishB. is there any other class with which I could do what I want? – Vasyl Havrylyuk Jul 05 '19 at 06:49
  • Ok, understand... You could have a look at [this question](https://stackoverflow.com/questions/28504504/modify-file-using-files-lines). It is about streaming the lines of a file and modifiying them if desired, but I don't know if it meets all your requirements. – deHaar Jul 05 '19 at 06:53
  • 1
    @0xefro Check with this class RandomAccessFile whether there is an API available for modifying a line ? Refer to this link : https://netjs.blogspot.com/2016/02/how-to-read-file-from-last-line-in-java.html – Anish B. Jul 05 '19 at 06:58
  • `RandomAccessFile` does have a `write` method, but to be able to write to an arbitrary position, you'd need to know the number of bytes until the position you need. See [this answer](https://stackoverflow.com/a/6012645/6803997) for potentially relevant information. – Austin Schaefer Jul 05 '19 at 07:29
  • @AustinSchäfer yeah mate, I was testing with it and it doesnt work for me, because it replaces bytes instead adding them on the specific line, I was looking for and its possible do it by rewritting all the file, its a bad option, because if the file has 20000 lines (for example) and I need modify online the last lines we lose performance – Vasyl Havrylyuk Jul 05 '19 at 09:41
  • This is not a trivial task as “inserting bytes” means “actually overwriting the entire contents from this position to the end”. Besides that, when you ask a question about a 3rd party library class, you should name that library. – Holger Jul 10 '19 at 15:02

0 Answers0