Java BufferedReader Read readFile(String absoluteFilePath)

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

Description

Read the contents of a file

License

Open Source License

Parameter

Parameter Description
absoluteFilePath : The absolute path of the file from which to read.

Return

: The contents of the file as a String.

Declaration

public static String readFile(String absoluteFilePath) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009 Red Hat, Inc./*from ww w  .ja  v a2 s  .co  m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Red Hat - initial API and implementation
 *******************************************************************************/

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 contents of a file
     * @param absoluteFilePath : The absolute path of the file from which to read.
     * @return : The contents of the file as a String.
     */
    public static String readFile(String absoluteFilePath) {

        try (BufferedReader bw = new BufferedReader(new FileReader(
                new File(absoluteFilePath)))) {
            String output = ""; //$NON-NLS-1$
            String tmp = ""; //$NON-NLS-1$
            while ((tmp = bw.readLine()) != null) {
                output += tmp + "\n"; //$NON-NLS-1$
            }
            bw.close();

            return output;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. readFile(final String name)
  2. readFile(final String path)
  3. readFile(final String pFile)
  4. readFile(List fieldNames, String filename)
  5. readFile(Reader reader)
  6. readFile(String aFileName)
  7. readFile(String directory)
  8. readFile(String directory, String fileName)
  9. readFile(String file)