Java File Read getFileContent(File pFile)

Here you can find the source of getFileContent(File pFile)

Description

Read the file and returns its content

License

Apache License

Parameter

Parameter Description
pFile File

Return

String the content

Declaration

public static String getFileContent(File pFile) 

Method Source Code

//package com.java2s;
/**//  ww w .j a  v  a  2s .  com
 * Copyright 2014 Simeon GIROUSSE
 * 
 * Licensed 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.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Main {
    /**
     * Read the file and returns its content
     * 
     * @param pFile File
     * @return String the content
     */
    public static String getFileContent(File pFile) {
        String content = new String();
        FileReader fileReader = null;

        try {
            fileReader = new FileReader(pFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }

        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String line = null;
        try {
            while ((line = bufferedReader.readLine()) != null) {
                content += line;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

        try {
            fileReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return content;
    }
}

Related

  1. getFileContent(File file, int bufferSize)
  2. getFileContent(final File f)
  3. getFileContent(final File file)
  4. getFileContent(final File srcFile)
  5. getFileContent(final String fileName)