Java BufferedReader Read readFile(final File file)

Here you can find the source of readFile(final File file)

Description

read File

License

Open Source License

Declaration

public static String readFile(final File file) 

Method Source Code


//package com.java2s;
/*//from ww w . j a v a 2  s  .  co  m
 * Fynche - a Framework for Multiagent Computational Creativity
 * Copyright 2011 Josh Hansen
 * 
 * This file is part of the Fynche <https://github.com/joshhansen/fynche>.
 * 
 * Fynche is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 * 
 * Fynche 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 Affero General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with Fynche.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * If you have inquiries regarding any further use of Fynche, please
 * contact Josh Hansen <http://joshhansen.net/>
 */

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

import java.io.FileNotFoundException;

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

public class Main {
    public static String readFile(final String filename) {
        final StringBuilder file = new StringBuilder();
        try {
            final BufferedReader r = new BufferedReader(new FileReader(filename));
            String tmp = null;
            while ((tmp = r.readLine()) != null) {
                file.append(tmp);
                file.append("\n");
            }
            r.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file.toString();
    }

    public static String readFile(final File file) {
        StringBuilder text = new StringBuilder();
        try {
            BufferedReader r = new BufferedReader(new FileReader(file));
            String tmp = null;
            while ((tmp = r.readLine()) != null) {
                text.append(tmp);
                text.append("\n");
            }
            r.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return text.toString();
    }
}

Related

  1. readFile(File testPage)
  2. readFile(final File argFile)
  3. readFile(final File file)
  4. readFile(final File file)
  5. readFile(final File file)
  6. readFile(final File file)
  7. readFile(final File paramFile, final boolean paramWhitespaces)
  8. readFile(final String file)
  9. readFile(final String fileName)