partha
partha

Reputation: 2326

Reject the empty lines at end of text file while reading using Java.

I need to read a text file using java. Not a problem. But I need to reject the empty lines at the end of the file. The file is quite large, around a million or so lines. I need to process each line, one at a time. Even if they are empty.

But, if the empty lines are at the end of the file, then I need to reject it. Note that there can be multiple empty lines at the end of the file.

Any quick solutions? I almost want to write a FileUtility.trimEmptyLinesAndEnd(File input). But I cant help feeling that someone might have written something like this already.

Any help appreciated.

Note:

  1. I have read this link. Java: Find if the last line of a file is empty. But this is not what I am trying to do. I need to reject multiple empty lines.

Upvotes: 1

Views: 1995

Answers (4)

user9435915
user9435915

Reputation:

Just read the File backwards. Starting from the first line you read, refrain from processing all blank lines you encounter.

Starting with the first non-blank line you encounter, and thereafter, process all lines whether or not they're blank.

The problem is "intractable" wrt to a neat solution if you read the File forward since you can never know if at some point after a long run of blank lines there might yet be a non-blank line.

If the processing of lines in order, first-to-last, matters, then there is no neat solution and anything like what you have now is about what there is.

Upvotes: 0

partha
partha

Reputation: 2326

Thanks for all the responses. I think both Vash - Damian Leszczyński and forgivenson cracked the pseudocode for this problem. I have taken that forward and am providing here the Java code for people who come looking for an answer after me.

@Test
public void test() {

    BufferedReader br = null;

    try {

        String sCurrentLine;
        StringBuffer fileContent = new StringBuffer();
        int consecutiveEmptyLineCounter = 0;

        br = new BufferedReader(new FileReader("D:\\partha\\check.txt"));

        while ((sCurrentLine = br.readLine()) != null) {

            // if this is not an empty line
            if (!(sCurrentLine.trim().length() == 0)) {

                // if there are no empty lines before this line.
                if (!(consecutiveEmptyLineCounter > 0)) {

                    // It is a non empty line, with non empty line prior to this
                    // Or it is the first line of the file.
                    // Don't do anything special with it.
                    // Appending "|" at the end just for ease of debug.
                    System.out.println(sCurrentLine + "|");
                } else {

                    // This is a non empty line, but there were empty lines before this.
                    // The consecutiveEmptyLineCounter is > 0
                    // The "fileContent" already has the previous empty lines.
                    // Add this non empty line to "fileContent" and spit it out.

                    fileContent.append(sCurrentLine);
                    System.out.println(fileContent.toString() + "@");

                    // and by the way, the counter of consecutive empty lines has to be reset.
                    // "fileContent" has to start from a clean slate.
                    consecutiveEmptyLineCounter = 0;
                    fileContent = new StringBuffer();
                }
            } else {
                // this is an empty line

                // Don't execute anything on it.
                // Just keep it in temporary "fileContent"
                // And count up the consecutiveEmptyLineCounter
                fileContent.append(sCurrentLine);
                fileContent.append(System.getProperty("line.separator"));
                consecutiveEmptyLineCounter++;

            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)
                br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

Thanks for all the help.

And, what I have provided here is just one solution. If someone comes across something more clever, please share. I can't shake the feeling off that there should be some FileUtils.trimEmptyLinesAtEnd() sort of method somewhere.

Upvotes: 1

You have to read all lines in your file. You can introduce a guarding that will store the value of last not empty line. At the end return the subset from zero to guardian.

In case you have a stream process.

read line
if empty 
 increase empty lines counter
else 
 if there was some empty lines
   yield fake empty lines that counter store 
   reset counter 
 yield line

Upvotes: 1

forgivenson
forgivenson

Reputation: 4445

When you find an empty line, increment a counter for the number of empty lines. If the next line is also empty, increment the counter. If you reach the end of the file, just continue on with what you want to do (ignoring the empty lines you found). If you reach a non-empty line, first do whatever you do to process an empty line, repeating it for each empty line you counted. Then process the non-empty line as normal, and continue through the file. Also, don't forget to reset the empty line counter to zero.

Pseudo code:

emptyLines = 0;
while (the file has a next line) {
    if (line is empty) {
        emptyLines++;
    } else {
        if (emptyLines > 0) {
            for (i = 0; i < emptyLines; i++) {
                process empty line;
            }
            emptyLines = 0;
        }
        process line;
    }
}

Upvotes: 2

Related Questions