Java Path File Read nio read(String filePath)

Here you can find the source of read(String filePath)

Description

read

License

Open Source License

Parameter

Parameter Description
filePath the path to the file.

Return

the contents of the file in filePath.

Declaration

public static String read(String filePath) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2008-2009 SWTBot Committers and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * /*from ww w.ja  v a2s .  c  o  m*/
 * Contributors:
 *     Ketan Padegaonkar - initial API and implementation
 *******************************************************************************/

import java.io.*;
import java.net.URL;
import java.nio.charset.Charset;

public class Main {
    /**
     * @param filePath the path to the file.
     * @return the contents of the file in filePath.
     */
    public static String read(String filePath) {
        return read(new File(filePath));
    }

    /**
     * @param file the file to read from.
     * @return the contents of the file.
     */
    public static String read(File file) {
        try {
            return read(new FileInputStream(file));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @param in the input stream to read from.
     * @return the contents of the inputstream.
     */
    public static String read(InputStream in) {
        return read(new InputStreamReader(in, Charset.forName("UTF-8")));
    }

    /**
     * @param url the URL to read from.
     * @return the contents of the url.
     */
    public static String read(URL url) {
        try {
            return read(url.openStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @param in the reader to read from.
     * @return the contents of the reader.
     */
    public static String read(Reader in) {
        StringBuffer buffer = new StringBuffer();
        try {
            while (in.ready()) {
                buffer.append((char) in.read());
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            close(in);
        }
        return buffer.toString();
    }

    private static void close(Closeable c) {
        if (c != null) {
            try {
                c.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

Related

  1. openBufferedReader(String pathOrUrl)
  2. openForReadingAndWriting(String filePath)
  3. openResourceReader(Class resourceOrigin, String resourcePath)
  4. read(String aPathString)
  5. read(String filePath)
  6. read(String path)
  7. readAllLines(Path path)
  8. readAllLines(String path)
  9. readAllLinesOrExit(Path path)