Java Text File Read Line readLines(String lines)

Here you can find the source of readLines(String lines)

Description

Return a LinkedList of the lines in a string that aren't comments.

License

Open Source License

Parameter

Parameter Description
lines A String containing the lines to be separated.

Return

A LinkedList of the lines that aren't comments.

Declaration

public static LinkedList<String> readLines(String lines)
        throws IOException 

Method Source Code

//package com.java2s;
/* Utilities used to manipulate strings.

 Copyright (c) 2002-2013 The Regents of the University of California.
 All rights reserved./*from  w  w w. j  a v a 2 s  .com*/
 Permission is hereby granted, without written agreement and without
 license or royalty fees, to use, copy, modify, and distribute this
 software and its documentation for any purpose, provided that the above
 copyright notice and the following two paragraphs appear in all copies
 of this software.

 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.

 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
 ENHANCEMENTS, OR MODIFICATIONS.

 PT_COPYRIGHT_VERSION_2
 COPYRIGHTENDKEY

 */

import java.io.BufferedReader;

import java.io.IOException;

import java.io.StringReader;

import java.util.LinkedList;

public class Main {
    /** Return a LinkedList of the lines in a string that aren't comments.
     * @param lines A String containing the lines to be separated.
     * @return A LinkedList of the lines that aren't comments.
     * @exception IOException If thrown when reading from the input String.
     */
    public static LinkedList<String> readLines(String lines)
            throws IOException {
        BufferedReader bufferedReader = null;
        LinkedList<String> returnList = new LinkedList<String>();
        String line;
        bufferedReader = new BufferedReader(new StringReader(lines));
        try {
            // Read line by line, skipping comments.
            while ((line = bufferedReader.readLine()) != null) {
                line = line.trim();
                if (!(line.length() == 0 || line.startsWith("/*") || line
                        .startsWith("//"))) {
                    returnList.add(line);
                }
            }
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException ex) {
                    // Ignore
                    ex.printStackTrace();
                }
            }
        }
        return returnList;
    }
}

Related

  1. readLines(String fileName)
  2. readLines(String filePath)
  3. readLines(String filePath)
  4. readLines(String filePath)
  5. readLines(String fullFileName)
  6. readLines(String string)
  7. readLines(String targetPath)
  8. readLines(String text)
  9. readLines(String text)