Java BufferedReader Create getBufferedReader(String directory, String filename)

Here you can find the source of getBufferedReader(String directory, String filename)

Description

Returns a BufferedReader.

License

Open Source License

Parameter

Parameter Description
filename the name of the file to read from (can be .gz)
the directory that the file is in

Return

BufferedReader to read from the given file name

Declaration

public static BufferedReader getBufferedReader(String directory, String filename) throws IOException 

Method Source Code


//package com.java2s;
/* This file is part of the Joshua Machine Translation System.
 * /*from www  .j  a  v  a 2 s.com*/
 * Joshua is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

import java.io.*;

import java.util.zip.*;

public class Main {
    public static final String FILE_ENCODING = "utf8";

    /**
     * Returns a BufferedReader. Handles gzipped files.
     * 
     * @param filename the name of the file to read from (can be .gz)
     * @param the directory that the file is in
     * @return BufferedReader to read from the given file name 
     * @exception IOException if a stream cannot be created
     */
    public static BufferedReader getBufferedReader(String directory, String filename) throws IOException {
        // look to see if an gzipped version of the file exists.
        if (!exists(directory, filename) && exists(directory, filename + ".gz")) {
            filename = filename + ".gz";
        }
        InputStream stream = new FileInputStream(new File(directory, filename));
        if (filename.endsWith(".gz")) {
            stream = new GZIPInputStream(stream);
        }
        return new BufferedReader(new InputStreamReader(stream, FILE_ENCODING));
    }

    /**
     * Returns a BufferedReader. Handles gzipped files.  Also supports reading from 
     * STDIN if "-" is specified as the filename.
     * 
     * @param filename the name of the file to read from (can be .gz)
     * @return BufferedReader to read from the given file name 
     * @exception IOException if a stream cannot be created
     */
    public static BufferedReader getBufferedReader(String filename) throws IOException {
        if (filename.equals("-")) {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            return in;
        }
        InputStream stream = new FileInputStream(new File(filename));
        if (filename.endsWith(".gz")) {
            stream = new GZIPInputStream(stream);
        }
        return new BufferedReader(new InputStreamReader(stream, FILE_ENCODING));
    }

    /**
     * Checks whether a specified file exists in a specified directory.
     * 
     * @param directory The directory to look in for the specified file.
     * @param filename The name of the file to check for.
     * @return <code>true</code> if a file with the specified filename exists in the specified directory, <code>false</code> otherwise.
     */
    public static boolean exists(String directory, String filename) {
        File file = new File(directory, filename);
        return file.exists();
    }

    /**
     * Checks whether the file exists.
     */
    public static boolean exists(String filename) {
        File file = new File(filename);
        return file.exists();
    }
}

Related

  1. getBufferedReader(InputStream inputStream)
  2. getBufferedReader(InputStream inputStream, String charsetName)
  3. getBufferedReader(InputStream stream, String charset)
  4. getBufferedReader(Reader r)
  5. getBufferedReader(Reader rd)
  6. getBufferedReader(String file)
  7. getBufferedReader(String filename)
  8. getBufferedReader(String filename, String encoding)
  9. getBufferedReader(String nombreFichero)