Java BufferedReader Read Line readLineByLine(File file, char[][] terminators, boolean keepTerminators, Consumer readLineListener)

Here you can find the source of readLineByLine(File file, char[][] terminators, boolean keepTerminators, Consumer readLineListener)

Description

Reads a file line by line using some supplied terminators to define when a line ends.

License

Open Source License

Parameter

Parameter Description
file the file to read
terminators the terminators used to determine a line end
keepTerminators whether to keep the terminators in the output line string
readLineListener what to do with each line

Declaration

public static void readLineByLine(File file, char[][] terminators, boolean keepTerminators,
        Consumer<String> readLineListener) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;

import java.util.Arrays;
import java.util.function.Consumer;

public class Main {
    /**//from   ww  w . j  av  a2s.  com
     * Reads a file line by line using some supplied terminators to define when a line ends.
     *
     * @param file  the file to read
     * @param terminators the terminators used to determine a line end
     * @param keepTerminators whether to keep the terminators in the output line string
     * @param readLineListener what to do with each line
     */
    public static void readLineByLine(File file, char[][] terminators, boolean keepTerminators,
            Consumer<String> readLineListener) {
        try {
            StringBuilder sb = new StringBuilder();
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            String line;
            while ((line = readUntil(br, terminators, sb, keepTerminators)) != null) {
                readLineListener.accept(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String readUntil(BufferedReader br, char[][] terminators, StringBuilder sb,
            boolean keepTerminators) throws IOException {

        boolean firstRead = true;
        boolean keepReading = true;

        while (keepReading) {
            int c = br.read();
            if (c == -1) {
                //if it is the first loop through the current readUntil and we get EOF, then return
                if (firstRead) {
                    return null;
                }
                //otherwise we got EOF whilst we have some string in the sb, so return the string
                break;
            }
            if (firstRead) {
                firstRead = false;
            }
            //append
            sb.append((char) c);
            //check for the terminating characters
            for (char[] terminator : terminators) {
                //the buffer has to have at least that same number of characters
                if (sb.length() >= terminator.length) {
                    //check the last characters of the buffer for containing the terminators
                    int srcEnd = sb.length();
                    if (srcEnd > 0) {
                        int srcBegin = srcEnd - terminator.length;
                        char[] lastChars = new char[terminator.length];
                        sb.getChars(srcBegin, srcEnd, lastChars, 0);
                        //check if we have terminator
                        if (Arrays.equals(lastChars, terminator)) {
                            if (!keepTerminators) {
                                //change the output length to not include the terminators
                                sb.setLength(sb.length() - terminator.length);
                            }
                            //stop reading if we found a terminator
                            keepReading = false;
                            //don't look for anymore terminators
                            break;
                        }
                    }
                }
            }
        }
        String out = sb.toString();
        sb.setLength(0);
        return out;
    }
}

Related

  1. readLine(String prompt)
  2. readLine(String prompt, boolean force)
  3. readLine(String s)
  4. readLine(String s)
  5. readLine(String value, BufferedReader procout)
  6. readLineFile(File file)
  7. readLineFile(String filePath)
  8. readLineFromCommand(String command[])
  9. readLineFromConsole()