Java BufferedReader Read readFile(String fileName, String commentFlag)

Here you can find the source of readFile(String fileName, String commentFlag)

Description

read File

License

Open Source License

Parameter

Parameter Description
fileName a parameter
commentFlag The leading character indicating a line of comment.

Return

The content, preferably plain text, of the file "fileName", with the comments ignored.

Declaration

public static StringBuffer readFile(String fileName, String commentFlag) 

Method Source Code

//package com.java2s;
/*//  ww  w  .j av a2s .  com
 JavaRAP: a freely-available JAVA anaphora resolution implementation
 of the classic Lappin and Leass (1994) paper:
    
 An Algorithm for Pronominal Anaphora Resolution.
 Computational Linguistics, 20(4), pp. 535-561.
    
 Copyright (C) 2005,2006  Long Qiu
    
 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.*;

public class Main {
    public static StringBuffer readFile(String fileName) {
        return readFile(fileName, null);
    }

    /**
     * @param fileName
     * @param commentFlag The leading character indicating a line of comment.
     * @return The content, preferably plain text, of the file "fileName", with the comments ignored.
     */
    public static StringBuffer readFile(String fileName, String commentFlag) {
        StringBuffer sb = new StringBuffer();
        try {
            BufferedReader in = new BufferedReader(new FileReader(fileName));
            String s;
            while ((s = in.readLine()) != null) {
                if (commentFlag != null && s.trim().startsWith(commentFlag)) {
                    //ignore this line
                    continue;
                }
                sb.append(s);
                //sb.append("/n"); to make it more platform independent (Log July 12, 2004)
                sb.append(System.getProperty("line.separator"));
            }
            in.close();
        } catch (IOException ex) {
            ex.printStackTrace();
            System.exit(-1);
        }
        return sb;

    }
}

Related

  1. readFile(String FileName)
  2. readFile(String fileName)
  3. readFile(String filename, boolean print)
  4. readFile(String filename, boolean skipEmptyLine)
  5. readFile(String fileName, String basePath)
  6. readFile(String fileName, String commentFlag)
  7. readFile(String fileNm)
  8. readFile(String filePath)
  9. readFile(String filePath)