Java Text File Read by Encoding readTextFile(File file, String encoding)

Here you can find the source of readTextFile(File file, String encoding)

Description

Reads a text file.

License

Open Source License

Parameter

Parameter Description
file the text file
encoding the encoding of the textfile

Exception

Parameter Description
FileNotFoundException when the file was not found
IOException when file could not be read.

Return

the lines of the text file

Declaration

public static String[] readTextFile(File file, String encoding) throws FileNotFoundException, IOException 

Method Source Code


//package com.java2s;
/*//from   w  w w .ja  va 2s  .c  o m
 * Created on 14-Jan-2004 at 16:07:22.
 *
 * Copyright (c) 2004-2005 Robert Virkus / Enough Software
 *
 * This file is part of J2ME Polish.
 *
 * J2ME Polish 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 2 of the License, or
 * (at your option) any later version.
 * 
 * J2ME Polish 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 J2ME Polish; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Commercial licenses are also available, please
 * refer to the accompanying LICENSE.txt or visit
 * http://www.j2mepolish.org for details.
 */

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

import java.io.FileReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.ArrayList;

public class Main {
    /**
     * Reads a text file.
     *  
     * @param fileName the name of the text file
     * @return the lines of the text file
     * @throws FileNotFoundException when the file was not found
     * @throws IOException when file could not be read.
     */
    public static String[] readTextFile(String fileName) throws FileNotFoundException, IOException {
        return readTextFile(new File(fileName));
    }

    /**
     * Reads a text file.
     *  
     * @param file the text file
     * @return the lines of the text file
     * @throws FileNotFoundException when the file was not found
     * @throws IOException when file could not be read.
     */
    public static String[] readTextFile(File file) throws FileNotFoundException, IOException {
        ArrayList lines = new ArrayList();
        BufferedReader in = new BufferedReader(new FileReader(file));
        String line;
        while ((line = in.readLine()) != null) {
            lines.add(line);
        }
        in.close();
        return (String[]) lines.toArray(new String[lines.size()]);
    }

    /**
     * Reads a text file.
     *  
     * @param file the text file
     * @param encoding the encoding of the textfile
     * @return the lines of the text file
     * @throws FileNotFoundException when the file was not found
     * @throws IOException when file could not be read.
     */
    public static String[] readTextFile(File file, String encoding) throws FileNotFoundException, IOException {
        ArrayList lines = new ArrayList();
        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
        String line;
        while ((line = in.readLine()) != null) {
            lines.add(line);
        }
        in.close();
        return (String[]) lines.toArray(new String[lines.size()]);
    }
}

Related

  1. readFileAsString(File file, String encoding)
  2. readFileAsString(File file, String encoding)
  3. readTextFile(File f, String fileEncoding)
  4. readTextFile(File file, String encoding)
  5. readTextFile(File path, String encoding)
  6. readTextFile(InputStream in, String encoding)
  7. readTextFile(InputStream in, String encoding)