Here you can find the source of newBufferedReader(Path path)
Parameter | Description |
---|---|
path | to be read |
Parameter | Description |
---|---|
IOException | an exception |
public static BufferedReader newBufferedReader(Path path) throws IOException
//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; } }