Java BufferedReader Read readFile(String filePath)

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

Description

Read file.

License

Open Source License

Parameter

Parameter Description
filePath the file path

Return

the string

Declaration

public static String readFile(String filePath) 

Method Source Code


//package com.java2s;
/*//from  w  w w. j  ava 2  s .  c  o  m
 * FileUtil.java
 * Copyright (c) 2014, CODEROAD, All Rights Reserved.
 *
 * This software is the confidential and proprietary information of CODEROAD
 * ("Confidential Information"). You shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement you entered into with
 * CODEROAD.
 */

import java.io.BufferedReader;
import java.io.File;

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

public class Main {
    /**
     * Read file.
     * 
     * @param filePath the file path
     * @return the string
     */
    public static String readFile(String filePath) {

        StringBuffer stringBuffer = null;
        try {
            FileReader fileReader = new FileReader(new File(filePath));
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            stringBuffer = new StringBuffer();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuffer.append(line);
                stringBuffer.append("\n");
            }
            fileReader.close();
            return stringBuffer.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuffer.toString();
    }
}

Related

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