Java FileReader Create readTextFile(String fileName)

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

Description

Reads the contents of the passed text file.

License

Open Source License

Parameter

Parameter Description
fileName The name of the file.

Exception

Parameter Description
IOException If we cannot write the file or create a stream.

Return

See above.

Declaration

public static String readTextFile(String fileName) throws IOException 

Method Source Code

//package com.java2s;
/*//from  www  .j  av a  2s  .c o m
 * org.openmicroscopy.shoola.util.file.IOUtil 
 *
 *------------------------------------------------------------------------------
 *  Copyright (C) 2006-2015 University of Dundee. All rights reserved.
 *
 *
 *  This program 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.
 *  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.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *------------------------------------------------------------------------------
 */

import java.io.BufferedReader;
import java.io.File;

import java.io.FileReader;
import java.io.IOException;

public class Main {
    /**
     * Reads the contents of the passed text file.
     * 
     * @param file The file to handle.
     * @return See above.
     * @throws IOException If we cannot write the file or create a stream.
     */
    public static String readTextFile(File file) throws IOException {
        StringBuffer contents = new StringBuffer();
        BufferedReader input = new BufferedReader(new FileReader(file));
        try {
            String line = null;
            while ((line = input.readLine()) != null) {
                contents.append(line);
                contents.append(System.getProperty("line.separator"));
            }
        } finally {
            input.close();
        }
        return contents.toString();
    }

    /**
     * Reads the contents of the passed text file.
     * 
     * @param fileName The name of the file.
     * @return See above.
     * @throws IOException If we cannot write the file or create a stream.
     */
    public static String readTextFile(String fileName) throws IOException {
        if (fileName == null)
            throw new IllegalArgumentException("No file name specified.");
        return readTextFile(new File(fileName));
    }
}

Related

  1. readTextFile(String file)
  2. readTextFile(String file)
  3. readTextFile(String file)
  4. readTextFile(String filename)
  5. readTextFile(String filename)
  6. readTextFile(String filename)
  7. readTextFile(String fileName)
  8. readTextFile(String fileName)
  9. readTextFile(String fileName)