Java File Content Read getFileContent(String fileName)

Here you can find the source of getFileContent(String fileName)

Description

Gets the content from a File as StringArray List.

License

Open Source License

Parameter

Parameter Description
fileName A file to read from.

Exception

Parameter Description
IOException an exception

Return

List of individual line of the specified file. List may be empty but not null.

Declaration

public static List<String> getFileContent(String fileName) throws IOException 

Method Source Code


//package com.java2s;
/*/*from   w  w w  . j  a v  a  2  s. c o m*/
 * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
 *
 * http://izpack.org/
 * http://izpack.codehaus.org/
 *
 * Copyright 2005 Marc Eppelmann
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.*;

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Gets the content from a File as StringArray List.
     *
     * @param fileName A file to read from.
     * @return List of individual line of the specified file. List may be empty but not
     *         null.
     * @throws IOException
     */
    public static List<String> getFileContent(String fileName) throws IOException {
        List<String> result = new ArrayList<String>();

        File aFile = new File(fileName);

        if (!aFile.isFile()) {
            //throw new IOException( fileName + " is not a regular File" );
            return result; // None
        }

        BufferedReader reader = null;

        try {
            reader = new BufferedReader(new FileReader(aFile));
        } catch (FileNotFoundException e) {
            // TODO handle Exception
            e.printStackTrace();

            return result;
        }

        String aLine = null;

        while ((aLine = reader.readLine()) != null) {
            result.add(aLine + "\n");
        }

        reader.close();

        return result;
    }
}

Related

  1. getFileContent(String filename)
  2. getFileContent(String fileName)
  3. getFileContent(String filename)
  4. getFileContent(String fileName)
  5. getFileContent(String fileName)
  6. getFileContent(String fileName)
  7. getFileContent(String filePath)
  8. getFileContent(String filePath)
  9. getFileContent(String filepath)