Java InputStreamReader Read All readAll(File file)

Here you can find the source of readAll(File file)

Description

read All

License

Open Source License

Declaration

public static String readAll(File file) throws IOException 

Method Source Code


//package com.java2s;
/*/* w  w w .ja  va2 s.  c o m*/
 * AutoRefactor - Eclipse plugin to automatically refactor Java code bases.
 *
 * Copyright (C) 2016-2017 Jean-No?l Rouvignac - initial API and implementation
 *
 * 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
 * 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 under LICENSE-GNUGPL.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution under LICENSE-ECLIPSE, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

public class Main {
    public static String readAll(File file) throws IOException {
        final StringBuilder sb = new StringBuilder();
        FileInputStream input = null;
        Reader reader = null;
        try {
            input = new FileInputStream(file);
            reader = new InputStreamReader(input, "UTF-8");
            int c = 0;
            while ((c = reader.read()) != -1) {
                sb.append((char) c);
            }
        } finally {
            if (input != null) {
                input.close();
            }
            if (reader != null) {
                reader.close();
            }
        }
        return sb.toString();
    }
}

Related

  1. readAll(InputStream input)
  2. readAll(InputStreamReader reader)
  3. readAll(Reader source)