Java BufferedReader Read readFile(String file)

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

Description

Read file.

License

Open Source License

Parameter

Parameter Description
file the file

Return

the string

Declaration

public static String readFile(String file) 

Method Source Code


//package com.java2s;
/*// ww  w. j  a v a  2  s  . c  o m
This file is part of PSFj.
    
PSFj is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
PSFj 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 General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with PSFj.  If not, see <http://www.gnu.org/licenses/>. 
    
   Copyright 2013,2014 Cyril MONGIS, Patrick Theer, Michael Knop
       
 */

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

public class Main {
    /**
     * Read file.
     *
     * @param file the file
     * @return the string
     */
    public static String readFile(String file) {
        String result = "";
        System.out.println("Reading file " + file + "...");
        try {
            FileReader reader = new FileReader(file);
            BufferedReader bReader = new BufferedReader(reader);
            String line = "";
            while (line != null) {

                line = bReader.readLine();
                if (line != null) {
                    result += line + "\n";
                }
            }

            bReader.close();
            reader.close();
            reader = null;
            bReader = null;

            System.gc();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("File read.");

        return result;
    }
}

Related

  1. readFile(String file)
  2. ReadFile(String file)
  3. readFile(String file)
  4. readFile(String file)
  5. readFile(String file)
  6. readFile(String file)
  7. readFile(String file, Boolean deleteOnExit)
  8. readFile(String fileDir)
  9. readFile(String filename)