Java BufferedReader Read readFile(final String fileName)

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

Description

read File

License

Open Source License

Parameter

Parameter Description
fileName a parameter

Declaration

private static String readFile(final String fileName) 

Method Source Code


//package com.java2s;
/*//from   w  ww.  jav  a  2 s. c  o m
 * Fast Code Plugin for Eclipse
 *
 * Copyright (C) 2008  Gautam Dev
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library 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 Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
 *
 * Plugin Home Page: http://fast-code.sourceforge.net/
 */

import java.io.BufferedReader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Main {
    /**
     * @param fileName
     * @return
     */
    private static String readFile(final String fileName) {

        final File file = new File(fileName);

        char[] buffer = null;

        try {
            final BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

            buffer = new char[(int) file.length()];

            int i = 0;
            int c = bufferedReader.read();

            while (c != -1) {
                buffer[i++] = (char) c;
                c = bufferedReader.read();
            }
        } catch (final FileNotFoundException e) {
        } catch (final IOException e) {
        }

        return new String(buffer);
    }
}

Related

  1. readFile(final File file)
  2. readFile(final File paramFile, final boolean paramWhitespaces)
  3. readFile(final String file)
  4. readFile(final String fileName)
  5. readFile(final String filename)
  6. readFile(final String filePath)
  7. readFile(final String name)
  8. readFile(final String path)
  9. readFile(final String pFile)