Java BufferedReader Read Line readLine(Reader rd)

Here you can find the source of readLine(Reader rd)

Description

Read a line from an InputStream in human-readable-form as a String.

License

Open Source License

Return

the line read or null if no single character could be read.

Declaration

public static String readLine(Reader rd) throws IOException 

Method Source Code

//package com.java2s;

import java.io.Reader;
import java.io.InputStream;
import java.io.IOException;

public class Main {
    /**// w w  w.  j a  v  a  2 s.com
     * Read a line from an InputStream in human-readable-form as a String.
     * Reading stops at LF ('\n') or EOF and skips CR ('\r').
     * @return the line read or null if no single character could be read.
     * @see #readLine(java.io.InputStream)
     */
    public static String readLine(InputStream is) throws IOException {
        String input = "";
        while (!Thread.interrupted()) {
            int ch = is.read();
            if (ch == -1)
                if ("".equals(input))
                    return null;
                else
                    break;
            if (ch == '\r')
                continue;
            if (ch == '\n')
                break;
            input += (char) ch;
        }
        return input;
    }

    /**
     * Read a line from an InputStream in human-readable-form as a String.
     * Reading stops at LF ('\n') or EOF and skips CR ('\r').
     * <p>Prefer using <pre>
     * BufferedReader rd = new BufferedReader(inner_reader);
     * // forget about using the inner reader since rd is buffered!
     * inner_reader = null;
     * String l = rd.readLine();
     * </pre>
     * whenever possible.
     * @return the line read or null if no single character could be read.
     * @see java.io.BufferedReader#readLine()
     */
    public static String readLine(Reader rd) throws IOException {
        String input = "";
        while (!Thread.interrupted()) {
            int ch = rd.read();
            if (ch == -1)
                if ("".equals(input))
                    return null;
                else
                    break;
            if (ch == '\r')
                continue;
            if (ch == '\n')
                break;
            input += (char) ch;
        }
        return input;
    }

    /**
     * Check whether two objects are equal.
     * <p>
     * This method is just a shortcut to simplify assertion statements.</p>
     * @preconditions a == b &rarr; a.equals(b)
     * @return an optimized version of a <span class="operator">==</span> <span class="keyword">null</span> <span class="operator">?</span> b <span class="operator">==</span> <span class="keyword">null</span> <span class="operator">:</span> a.equals(b).
     *  Namely a <span class="operator">==</span> b <span class="operator">||</span> (a <span class="operator">!=</span> <span class="keyword">null</span> <span class="operator">&&</span> a.equals(b)).
     * @postconditions a == b || (a != null && a.equals(b))
     * @see #hashCode(Object)
     */
    public static final boolean equals(Object a, Object b) {
        return a == b || (a != null && a.equals(b));
    }
}

Related

  1. readLine(InputStream in)
  2. readLine(InputStream in)
  3. readLine(InputStream input)
  4. readLine(InputStream input)
  5. readLine(RandomAccessFile wiki)
  6. readLine(String fileName)
  7. readLine(String fileName)
  8. readLine(String filename, String encoding)
  9. readLine(String filepath)