Java FileReader Create readTextFile(File file)

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

Description

Read a text file into a String array object.

License

Open Source License

Parameter

Parameter Description
file File object representing a text file.

Return

The file read into a String array object.

Declaration

public static String[] readTextFile(File file) 

Method Source Code

//package com.java2s;
/*//w  w  w .  java2 s.  co  m
This file is part of JFLICKS.
    
JFLICKS 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.
    
JFLICKS 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 JFLICKS.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

public class Main {
    /**
     * Read a text file into a String array object.
     *
     * @param file File object representing a text file.
     * @return The file read into a String array object.
     */
    public static String[] readTextFile(File file) {

        String[] result = null;

        ArrayList<String> work = new ArrayList<String>();
        if (file != null) {

            BufferedReader in = null;

            try {

                in = new BufferedReader(new FileReader(file));
                String line = null;
                while ((line = in.readLine()) != null) {
                    work.add(line);
                }

                result = (String[]) work.toArray(new String[work.size()]);
                in.close();
                in = null;

            } catch (IOException e) {

                result = null;

            } finally {

                if (in != null) {

                    try {

                        in.close();

                    } catch (IOException ex) {

                        throw new RuntimeException(ex);
                    }
                }
            }
        }

        return (result);
    }
}

Related

  1. getFileReader(String sFileName)
  2. getReader(final File file)
  3. readTextFile(File aFile)
  4. readTextFile(File f)
  5. readTextFile(File f)
  6. readTextFile(File file)
  7. readTextFile(File file)
  8. readTextFile(File file)
  9. readTextFile(File file)