Java BufferedReader Read readFile(String filePath)

Here you can find the source of readFile(String filePath)

Description

read File

License

Open Source License

Declaration

public static String readFile(String filePath) throws IOException 

Method Source Code

//package com.java2s;
/*//from  w ww  .  j  av a 2 s .  c o  m
 *Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *WSO2 Inc. 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.BufferedReader;

import java.io.FileReader;
import java.io.IOException;

public class Main {
    public static String readFile(String filePath) throws IOException {
        BufferedReader reader = null;
        FileReader fileReader = null;
        StringBuilder stringBuilder;
        String line;
        String ls;
        try {
            stringBuilder = new StringBuilder();
            fileReader = new FileReader(filePath);
            reader = new BufferedReader(fileReader);

            ls = System.getProperty("line.separator");

            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
                stringBuilder.append(ls);
            }

        } finally {
            if (fileReader != null) {
                fileReader.close();
            }
            if (reader != null) {
                reader.close();
            }
        }
        return stringBuilder.toString();
    }
}

Related

  1. readFile(String fileName, String commentFlag)
  2. readFile(String fileNm)
  3. readFile(String filePath)
  4. readFile(String filePath)
  5. readFile(String filePath)
  6. readFile(String filePath)
  7. readFile(String filePath)
  8. readFile(String filePath, Boolean replaceNewLineWithBr)
  9. readFile(String fullPath)