Java File Read by Charset toString(final Path filePath, final Charset encoding)

Here you can find the source of toString(final Path filePath, final Charset encoding)

Description

Read the contents of a file into String using specified encoding.

License

Open Source License

Parameter

Parameter Description
filePath the file path
encoding encoding of file

Exception

Parameter Description
IOException if the file does not exist or cannot be read.

Return

The file contents as string

Declaration

public static String toString(final Path filePath, final Charset encoding) throws IOException 

Method Source Code


//package com.java2s;
/*//from   w w  w  .ja  va  2 s .c  om
 *    Geotoolkit.org - An Open Source Java GIS Toolkit
 *    http://www.geotoolkit.org
 *
 *    (C) 2008-2012, Open Source Geospatial Foundation (OSGeo)
 *    (C) 2009-2012, Geomatys
 *
 *    This library 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;
 *    version 2.1 of the License.
 *
 *    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.
 */

import java.io.*;

import java.nio.charset.Charset;
import java.nio.file.*;

import java.util.*;

public class Main {
    /**
     * Default charset with UTF-8
     */
    public static final Charset UTF8_CHARSET = Charset.forName("UTF-8");

    /**
     * Read the contents of a file into String using UTF-8 encoding.
     *
     * @param filePath the file path
     * @return The file contents as string
     * @throws IOException if the file does not exist or cannot be read.
     */
    public static String toString(final Path filePath) throws IOException {
        return toString(filePath, UTF8_CHARSET);
    }

    /**
     * Read the contents of a file into String using specified encoding.
     *
     * @param filePath the file path
     * @param encoding encoding of file
     * @return The file contents as string
     * @throws IOException if the file does not exist or cannot be read.
     */
    public static String toString(final Path filePath, final Charset encoding) throws IOException {
        final List<String> lines = Files.readAllLines(filePath, encoding);

        final StringBuilder sb = new StringBuilder();
        for (String line : lines) {
            sb.append(line).append('\n');
        }
        return sb.toString();
    }

    /**
     * Read the contents of a stream into String using UTF-8 encoding and close the Stream.
     *
     * @param stream input steam
     * @return The file contents as string
     * @throws IOException if the file does not exist or cannot be read.
     */
    public static String toString(final InputStream stream) throws IOException {
        return toString(stream, UTF8_CHARSET);
    }

    /**
     * Read the contents of a stream into String with specified encoding and close the Stream.
     *
     * @param stream input steam
     * @return The file contents as string
     * @throws IOException if the file does not exist or cannot be read.
     */
    public static String toString(final InputStream stream, final Charset encoding) throws IOException {

        final StringBuilder sb = new StringBuilder();
        try (BufferedReader br = new BufferedReader(new InputStreamReader(stream, encoding))) {
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line).append('\n');
            }
        } finally {
            stream.close();
        }
        return sb.toString();
    }

    /**
     * Closes the given stream if it is closeable, or do nothing otherwise. Closeable
     * objects are any instance of {@link Closeable}.
     *
     * @param  stream The object to close if it is closeable.
     * @throws IOException If an error occurred while closing the stream.
     *
     * @since 3.07
     */
    public static void close(final Object stream) throws IOException {
        if (stream instanceof Closeable) {
            ((Closeable) stream).close();
        }
        // On JDK6, we needed an explicit check for ImageInputStream.
        // Since JDK7, this is not needed anymore.
    }
}

Related

  1. setText(File file, Charset charset, String text)
  2. stringToFile(final String s, final File f, final Charset c)
  3. stringToFile(final String string, final Path path, final Charset charset)
  4. toFiles(@Nonnull Process p, @Nonnull Charset charset)
  5. toString(File file, String charset)
  6. utf8Decoder(CodingErrorAction codingErrorAction, Charset fileCharset)