Reading File Issue

I have a text file. I read this file line by line and compare it with a list.(I do not want to keep the entire file in a list it has more almost milion rows).When the list matches the line from the file, I need to keep the first 4 line from the current line and the next 4 line from the current file. I succeded to keep the next 4 lines, ho can I keep the first 4 lines??? Can you guys help me? Thank you

public void compareTwoLists(List a, File f){ //read 4 before and after and compare
        // List a = searchArrayList
        // List b = listContainingTextArea
        List<String> l = new ArrayList<String>();
        String line = "";

    int count = 0;
    int lineNo;
    boolean b = false;
    BufferedReader textArea = null;
    try{
        textArea = new BufferedReader(new FileReader(f));

        for (int i = 0; i < a.size(); i++){
            System.out.println("test");
            b = false;
            while((line = textArea.readLine()) != null){

                if (a.get(i).toString().trim().toLowerCase().equals(line.toString().trim().toLowerCase())){
                    b = true;                        
                }
                if (b){
                    count++;                                                
                    if(count <= 4){
                        l.add(line);
                        System.out.println(line);
                    }else{
                        b = false;
                    }
                }
            }
        }
    }
    catch(Exception e){
        System.out.println("No such file was created");
    }
    finally{
        try{
            if (textArea != null){
                textArea.close();
            }
        }
        catch(IOException ioex){
            System.out.println("An error to close the file was produced");
        }
    }
}

Answers


From what little information you provided, I think what you need to do is just keep a buffer of the previous unmatched 4 lines. You want it to basically be a FIFO (first in, first out) queue that never gets bigger than 4. As you add on the end, if its bigger than four, you also remove from the front and discard. You can do this efficiently with a LinkedList. Once you have a match, you can get the lines from the queue.


Just keep an array of size 4. Read line 1,store it in the array at 0 Readline2,store it in the array at 1 Read line 3,store it in the array at 2 Read line 4,store it in the array at 3 If by now you didn't get a match than you don't need the first line anymore. So Read line 5, store it in the array at 0. Read line 6,store it in the array at 1. That way you can always keep 4 lines before the current line. If you want the lines ordered,than you will have to shift the array whenever you need to replace a line in the array and keep adding at index 3(only if the array is full!!)


Need Your Help

Memory Management Confusion C++

c++ pointers memory-management dynamic

I'm having trouble understanding some basic memory management principles in C++. This code is part of a loop that is part of a function that reads in a maze file into a 2D vector.

css text not aligned

html css

I have a problem with centering the text inside of my div. Can someone please help me align this text to the center of the box