Java Path File Read nio newBufferedReader(Path path)

Here you can find the source of newBufferedReader(Path path)

Description

This method is able to determine whether a file is GZipped and return a BufferedReader in any case

License

Apache License

Parameter

Parameter Description
path to be read

Exception

Parameter Description
IOException an exception

Return

BufferedReader object

Declaration

public static BufferedReader newBufferedReader(Path path) throws IOException 

Method Source Code


//package com.java2s;
/*/*from   ww  w . j  ava 2  s  .c  o m*/
 * Copyright 2015 OpenCB
 *
 * 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.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.zip.GZIPInputStream;

public class Main {
    /**
     * This method is able to determine whether a file is GZipped and return a BufferedReader in any case
     * @param path to be read
     * @return BufferedReader object
     * @throws IOException
     */
    public static BufferedReader newBufferedReader(Path path) throws IOException {
        BufferedReader br = null;
        if (path.toFile().getName().endsWith(".gz")) {
            br = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(path.toFile()))));
        } else {
            br = Files.newBufferedReader(path, Charset.defaultCharset());
        }
        return br;
    }

    /**
     * This method is able to determine whether a file is GZipped and return a BufferedReader in any case
     * @param path to be read
     * @return BufferedReader object
     * @throws IOException
     */
    public static BufferedReader newBufferedReader(Path path, Charset charset) throws IOException {
        BufferedReader br = null;
        if (path.toFile().getName().endsWith(".gz")) {
            br = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(path.toFile()))));
        } else {
            br = Files.newBufferedReader(path, charset);
        }
        return br;
    }
}

Related

  1. loadStrings(Path thePath)
  2. loadStringsFromFile(String pathToFile)
  3. loadTestPackets(final Path path)
  4. loadTitles(Path rootDir, Set container)
  5. newBufferedReader(Path path)
  6. openBufferedReader(String pathOrUrl)
  7. openForReadingAndWriting(String filePath)
  8. openResourceReader(Class resourceOrigin, String resourcePath)
  9. read(String aPathString)