Java InputStreamReader Create readTextFileAsStringArray(final File file, final String charSet)

Here you can find the source of readTextFileAsStringArray(final File file, final String charSet)

Description

Read a text file into string array

License

Open Source License

Parameter

Parameter Description
file the file to be read, must not be null
charSet the charset to be used to decode strings, must not be null

Exception

Parameter Description
IOException it will be thrown if there is any transport problem

Return

a string array contains each text line as an element

Declaration

public static String[] readTextFileAsStringArray(final File file, final String charSet) throws IOException 

Method Source Code


//package com.java2s;
/*// ww  w.j  a  v  a  2s  .  co m
 * Copyright 2012 Igor Maznitsa (http://www.igormaznitsa.com)
 * 
 * This file is part of the JVM to Z80 translator project (hereinafter referred to as J2Z80).
 *
 * J2Z80 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.
 *
 * J2Z80 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 J2Z80.  If not, see <http://www.gnu.org/licenses/>. 
 */

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

import java.io.IOException;

import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Read a text file into string array
     * @param file the file to be read, must not be null
     * @param charSet the charset to be used to decode strings, must not be null
     * @return a string array contains each text line as an element
     * @throws IOException it will be thrown if there is any transport problem
     */
    public static String[] readTextFileAsStringArray(final File file, final String charSet) throws IOException {
        final BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charSet));
        final List<String> readString = new ArrayList<String>(256);
        try {
            while (true) {
                final String line = reader.readLine();
                if (line == null) {
                    break;
                }
                readString.add(line);
            }
        } finally {
            silentlyClose(reader);
        }
        return readString.toArray(new String[readString.size()]);
    }

    /**
     * Silently close any closeable object
     *
     * @param closeableOne the object to be closed, it can be null
     */
    public static void silentlyClose(final Closeable closeableOne) {
        if (closeableOne != null) {
            try {
                closeableOne.close();
            } catch (IOException ex) {
            }
        }
    }
}

Related

  1. readTextFile(String fileName)
  2. readTextFile(String filename, int maxNumLines, boolean startAtBeginning)
  3. readTextFile(String filePath, String charset)
  4. readTextFile(String fullPathFilename)
  5. readTextFile(String realPath)