Java BufferedReader Create getReader(final File inputFile)

Here you can find the source of getReader(final File inputFile)

Description

Gets a Reader from the given file object.

License

Apache License

Parameter

Parameter Description
inputFile the input file.

Exception

Parameter Description
IOException Signals that an I/O exception has occurred.

Return

the reader.

Declaration

public static Reader getReader(final File inputFile) throws IOException 

Method Source Code

//package com.java2s;
/**//from   w w w  .  j  a  va2  s .co m
 * Copyright (C) 2007 Asterios Raptis
 *
 * 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.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.Reader;

public class Main {
    /**
     * Gets a Reader from the given file object.
     *
     * @param inputFile
     *            the input file.
     * @return the reader.
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static Reader getReader(final File inputFile) throws IOException {
        return getReader(inputFile, null, false);
    }

    /**
     * Gets a Reader from the given file object.
     *
     * @param inputFile
     *            the input file
     * @param encoding
     *            The encoding from the file.
     * @param createFile
     *            If true and the file does not exist it will be create a new file.
     * @return the reader
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static Reader getReader(final File inputFile, final String encoding, final boolean createFile)
            throws IOException {
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader reader = null;
        if (inputFile.exists()) {
            fis = new FileInputStream(inputFile);
        } else {
            if (createFile) {
                inputFile.createNewFile();
                fis = new FileInputStream(inputFile);
            } else {
                throw new FileNotFoundException("File " + inputFile.getName() + " does not exist.");
            }
        }
        if (null == encoding) {
            isr = new InputStreamReader(fis);
        } else {
            isr = new InputStreamReader(fis, encoding);
        }
        // create the bufferedreader
        reader = new BufferedReader(isr);

        return reader;
    }
}

Related

  1. getReader(File file)
  2. getReader(File file)
  3. getReader(File file)
  4. getReader(File inFile)
  5. getReader(File queryFile)
  6. getReader(final InputStream is, final String enc)
  7. getReader(final Object aReference, final String aResourceName)
  8. getReader(final String fileName)
  9. getReader(final String fileName)