Java InputStreamReader Read readFile(File file)

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

Description

read File

License

Open Source License

Declaration

public static String readFile(File file) throws FileNotFoundException, UnsupportedEncodingException 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Product: OSeB http://code.google.com/p/oseb                                *
 * Copyright (C) 2012 Mario Grigioni                                          *
 * This program is free software; you can redistribute it and/or modify it    *
 * under the terms version 2 of the GNU General Public License as published   *
 * by the Free Software Foundation. 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, write to the Free Software Foundation, Inc.,    *
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.                     *
 *****************************************************************************/

import java.io.BufferedReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

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

import java.io.UnsupportedEncodingException;

import java.util.ArrayList;

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

    /**// w  w w .  j  a va2  s  . com
     * readFile
     * Reads a file and return the lines into a string
     * @param  File
     * @return String dados
     * @throws FileNotFoundException
     * @throws UnsupportedEncodingException 
     */
    public static String readFile(File file, String encoding)
            throws FileNotFoundException, UnsupportedEncodingException {

        String[] array = readFile(file.toString(), encoding);
        StringBuilder dados = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            dados.append(array[i]);
        }

        return dados.toString();
    }

    public static String readFile(File file) throws FileNotFoundException, UnsupportedEncodingException {
        return readFile(file, UTF8);
    }

    /**
     * readFile
     * Reads a file and return the lines into a string array
     * @param  String FileName (FilePath)
     * @return String[] lines
     * @throws FileNotFoundException
     * @throws UnsupportedEncodingException 
     */
    public static String[] readFile(String FileName, String encoding)
            throws FileNotFoundException, UnsupportedEncodingException {

        ArrayList<String> list = new ArrayList<String>();

        FileInputStream stream = new FileInputStream(FileName);
        InputStreamReader streamReader = new InputStreamReader(stream, encoding);
        BufferedReader reader = new BufferedReader(streamReader);

        // Neste while lemos o arquivo linha a linha
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                list.add(line);
            }
            reader.close();
            streamReader.close();
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            reader = null;
            streamReader = null;
            stream = null;
        }

        String[] lines = new String[list.size()];
        list.toArray(lines);
        return lines;
    }

    public static String[] readFile(String FileName) throws FileNotFoundException, UnsupportedEncodingException {
        return readFile(FileName, UTF8);
    }
}

Related

  1. readFile(File f)
  2. readFile(File f)
  3. readFile(File file)
  4. readFile(File file)
  5. readFile(File file)
  6. readFile(File file)
  7. readFile(File file)
  8. readFile(File file)
  9. readFile(File file)