Java BufferedReader Read Line readLine(BufferedReader in)

Here you can find the source of readLine(BufferedReader in)

Description

read a line from "in", using readLine().

License

Open Source License

Parameter

Parameter Description
in a parameter

Exception

Parameter Description
IOException an exception

Return

complete line or null on end of file.

Declaration

public static String readLine(BufferedReader in) throws IOException 

Method Source Code


//package com.java2s;
/*/* ww  w  .j  av a2 s .  c om*/
 * Copyright (C) 2012  Krawler Information Systems Pvt Ltd
 * All rights reserved.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

import java.io.BufferedReader;

import java.io.IOException;

public class Main {
    /**
     * read a line from "in", using readLine(). A trailing '\\' on the line will
     * be treated as continuation and the next line will be read and appended to
     * the line, without the \\.
     * 
     * @param in
     * @return complete line or null on end of file.
     * @throws IOException
     */
    public static String readLine(BufferedReader in) throws IOException {
        String line;
        StringBuilder sb = null;

        while ((line = in.readLine()) != null) {
            if (line.length() == 0) {
                break;
            } else if (line.charAt(line.length() - 1) == '\\') {
                if (sb == null)
                    sb = new StringBuilder();
                sb.append(line.substring(0, line.length() - 1));
            } else {
                break;
            }
        }

        if (line == null) {
            if (sb == null) {
                return null;
            } else {
                return sb.toString();
            }
        } else {
            if (sb == null) {
                return line;
            } else {
                sb.append(line);
                return sb.toString();
            }
        }
    }
}

Related

  1. readLine()
  2. readLine()
  3. readLine()
  4. readLine(BufferedReader br)
  5. readLine(BufferedReader br, int maxlen)
  6. readLine(BufferedReader in)
  7. readline(BufferedReader in, BufferedReader err)
  8. readLine(BufferedReader input)
  9. readLine(BufferedReader reader)