Java InputStreamReader Create readTextFile(File inputFile)

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

Description

Reads a text file.

License

Apache License

Parameter

Parameter Description
inputFile File to read

Exception

Parameter Description
IOException On i/o error

Return

The contents of the file.
The lines will be separated by "\\n".

Declaration

public static String readTextFile(File inputFile) throws IOException 

Method Source Code


//package com.java2s;
/*// ww w .  j av a 2 s . c o m
 *   Copyright 2007 skynamics AG
 *
 *   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.BufferedReader;
import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main {
    /**
     * Reads a text file stream.
     *
     * @param in Inputstream
     * @return The contents of the file.<br>
     * The lines will be separated by "\\n".
     * @throws IOException On i/o error
     */
    public static String readTextFile(InputStream in) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));

        StringBuffer sb = new StringBuffer();
        String line;
        while ((line = reader.readLine()) != null) {
            if (sb.length() > 0) {
                sb.append('\n');
            }
            sb.append(line);
        }

        return sb.toString();
    }

    /**
     * Reads a text file.
     *
     * @param inputFileName Name of the file to read
     * @return The contents of the file.<br>
     * The lines will be separated by "\\n".
     * @throws IOException On i/o error
     */
    public static String readTextFile(String inputFileName) throws IOException {
        FileInputStream in = null;

        try {
            in = new FileInputStream(inputFileName);

            return readTextFile(in);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }
    }

    /**
     * Reads a text file.
     *
     * @param inputFile File to read
     * @return The contents of the file.<br>
     * The lines will be separated by "\\n".
     * @throws IOException On i/o error
     */
    public static String readTextFile(File inputFile) throws IOException {
        FileInputStream in = null;

        try {
            in = new FileInputStream(inputFile);

            return readTextFile(in);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }
    }
}

Related

  1. readTextFile(File f, int maxNumLines)
  2. readTextFile(File file)
  3. readTextFile(File file)
  4. readTextFile(File file, String charsetName)
  5. readTextFile(File fromFile)
  6. readTextFile(File path)
  7. readTextFile(final File f)
  8. readTextFile(final File file)
  9. ReadTextFile(InputStream is)