Return an Iterator for the lines in a File Reader. - Android File Input Output

Android examples for File Input Output:Text File

Description

Return an Iterator for the lines in a File Reader.

Demo Code

/*// w  w  w . j a va 2  s . c  o m
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 */
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.CharArrayWriter;
import java.io.Closeable;
import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class Main{
    /**
     * Return an Iterator for the lines in a <code>Reader</code>.
     * <p>
     * <code>LineIterator</code> holds a reference to the open <code>Reader</code> specified here. When you have finished with the iterator you should close the reader to free internal resources. This can be done by closing the reader directly, or by calling {@link LineIterator#close()} or {@link LineIterator#closeQuietly(LineIterator)}.
     * <p>
     * The recommended usage pattern is:
     * 
     * <pre>
     * try {
     *   LineIterator it = IOUtils.lineIterator(reader);
     *   while (it.hasNext()) {
     *     String line = it.nextLine();
     *     /// do something with line
     *   }
     * }
     * finally {
     *   IOUtils.closeQuietly(reader);
     * }
     * </pre>
     * 
     * @param reader
     *          the <code>Reader</code> to read from, not null
     * @return an Iterator of the lines in the reader, never null
     * @throws IllegalArgumentException
     *           if the reader is null
     * @since Commons IO 1.2
     */
    public static LineIterator lineIterator(Reader reader) {
        return new LineIterator(reader);
    }
    /**
     * Return an Iterator for the lines in an <code>InputStream</code>, using the character encoding specified (or default encoding if null).
     * <p>
     * <code>LineIterator</code> holds a reference to the open <code>InputStream</code> specified here. When you have finished with the iterator you should close the stream to free internal resources. This can be done by closing the stream directly, or by calling {@link LineIterator#close()} or {@link LineIterator#closeQuietly(LineIterator)}.
     * <p>
     * The recommended usage pattern is:
     * 
     * <pre>
     * try {
     *   LineIterator it = IOUtils.lineIterator(stream, &quot;UTF-8&quot;);
     *   while (it.hasNext()) {
     *     String line = it.nextLine();
     *     /// do something with line
     *   }
     * }
     * finally {
     *   IOUtils.closeQuietly(stream);
     * }
     * </pre>
     * 
     * @param input
     *          the <code>InputStream</code> to read from, not null
     * @param encoding
     *          the encoding to use, null means platform default
     * @return an Iterator of the lines in the reader, never null
     * @throws IllegalArgumentException
     *           if the input is null
     * @throws IOException
     *           if an I/O error occurs, such as if the encoding is invalid
     * @since Commons IO 1.2
     */
    public static LineIterator lineIterator(InputStream input,
            String encoding) throws IOException {
        Reader reader = null;
        if (encoding == null) {
            reader = new InputStreamReader(input);
        } else {
            reader = new InputStreamReader(input, encoding);
        }
        return new LineIterator(reader);
    }
}

Related Tutorials