Java InputStreamReader Read readFile(String fileName)

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

Description

read File

License

Open Source License

Declaration

public static String readFile(String fileName) throws IOException 

Method Source Code

//package com.java2s;
/**/*from   w  w w  . j ava 2 s. c o m*/
 * Copyright (C) 2015 WING, NUS and NUS NLP Group.
 * 
 * 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.BufferedReader;
import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static final String ENCODING = "UTF-8";

    public static String readFile(String fileName) throws IOException {
        return readFile(fileName, ENCODING);
    }

    public static String readFile(File file) throws IOException {
        return readFile(file, ENCODING);
    }

    public static String readFile(String fileName, String encoding) throws IOException {
        return readFile(new File(fileName), encoding);
    }

    public static String readFile(File file, String encoding) throws IOException {
        StringBuilder sb = new StringBuilder();

        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(new FileInputStream(file), encoding))) {
            char[] tmp = new char[256];
            int r = reader.read(tmp);
            while (r != -1) {
                sb.append(tmp, 0, r);
                r = reader.read(tmp);
            }
        }

        return sb.toString();
    }
}

Related

  1. readFile(String filename)
  2. readFile(String fileName)
  3. readFile(String filename)
  4. readFile(String filename)
  5. readFile(String fileName)
  6. readFile(String fileName)
  7. readFile(String filename)
  8. readFile(String filename)
  9. readfile(String filename)