Java FileReader Create readTextFile(File f)

Here you can find the source of readTextFile(File f)

Description

read Text File

License

Open Source License

Declaration

public static String readTextFile(File f) 

Method Source Code


//package com.java2s;
/*//ww w  .  j  a v  a  2s. c  o m
 Bandika  - A Java based modular Content Management System
 Copyright (C) 2009-2018 Michael Roennau
    
 This program 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.
 This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
 */

import java.io.*;

public class Main {
    public static String readTextFile(String path) {
        File f = new File(path);
        return readTextFile(f);
    }

    public static String readTextFile(File f) {
        StringBuilder sb = new StringBuilder();
        try {
            if (!f.exists()) {
                return "";
            }
            FileReader reader = new FileReader(f);
            char[] chars = new char[4096];
            int len = 4096;
            while (len > 0) {
                len = reader.read(chars, 0, 4096);
                if (len > 0) {
                    sb.append(chars, 0, len);
                }
            }
            reader.close();
        } catch (IOException e) {
            return "";
        }
        return sb.toString();
    }
}

Related

  1. getFileReader(String fileName)
  2. getFileReader(String filePath)
  3. getFileReader(String sFileName)
  4. getReader(final File file)
  5. readTextFile(File aFile)
  6. readTextFile(File f)
  7. readTextFile(File file)
  8. readTextFile(File file)
  9. readTextFile(File file)