Java File to String fileToString(final File file, final String charsetName)

Here you can find the source of fileToString(final File file, final String charsetName)

Description

Reads a file and returns it as a string.

License

Apache License

Parameter

Parameter Description
string a parameter
outputFileName a parameter

Declaration

public static String fileToString(final File file, final String charsetName) 

Method Source Code


//package com.java2s;
/*//from  w ww . ja  va2  s.  c o  m
 * Copyright 2016 Crown Copyright
 *
 * 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.BufferedInputStream;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    /**
     * Buffer to use
     */
    public static final int BUFFER_SIZE = 8192;
    public final static String DEFAULT_CHARSET_NAME = "UTF-8";

    public static String fileToString(final File file) {
        return fileToString(file, DEFAULT_CHARSET_NAME);
    }

    /**
     * Reads a file and returns it as a string.
     * 
     * @param string
     * @param outputFileName
     */
    public static String fileToString(final File file, final String charsetName) {
        return streamToString(fileToStream(file), charsetName);
    }

    /**
     * Convert a resource to a string.
     * 
     * @param stream
     *            thing to convert
     * @return the string
     */
    public static String streamToString(final InputStream stream) {
        return streamToString(stream, DEFAULT_CHARSET_NAME);
    }

    /**
     * Convert a resource to a string.
     * 
     * @param stream
     *            thing to convert
     * @return the string
     */
    public static String streamToString(final InputStream stream, boolean close) {
        return streamToString(stream, DEFAULT_CHARSET_NAME, close);
    }

    /**
     * Convert a resource to a string.
     * 
     * @param stream
     *            thing to convert
     * @return the string
     */
    public static String streamToString(final InputStream stream, final String charsetName) {
        return streamToString(stream, charsetName, true);
    }

    /**
     * Convert a resource to a string.
     * 
     * @param stream
     *            thing to convert
     * @return the string
     */
    public static String streamToString(final InputStream stream, final String charsetName, boolean close) {
        try {
            if (stream == null) {
                return null;
            }

            final ByteArrayOutputStream baos = streamToBuffer(stream, close);
            return baos.toString(charsetName);
        } catch (IOException ioEx) {
            // Wrap it
            throw new RuntimeException(ioEx);
        }
    }

    /**
     * Reads a file and returns it as a stream.
     * 
     * @param string
     * @param outputFileName
     */
    public static InputStream fileToStream(final File file) {
        try {
            return new BufferedInputStream(new FileInputStream(file));
        } catch (IOException ioEx) {
            // Wrap it
            throw new RuntimeException(ioEx);
        }
    }

    /**
     * @param stream
     *            to read (and close)
     * @return buffer
     */
    public static ByteArrayOutputStream streamToBuffer(final InputStream stream) {
        return streamToBuffer(stream, true);
    }

    /**
     * @param stream
     *            to read
     * @param close
     *            or not
     * @return buffer
     */
    public static ByteArrayOutputStream streamToBuffer(final InputStream stream, final boolean close) {
        if (stream == null) {
            return null;
        }
        final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        try {
            final byte[] buffer = new byte[BUFFER_SIZE];
            int len;
            while ((len = stream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, len);
            }
            if (close) {
                stream.close();
            }
            return byteArrayOutputStream;
        } catch (IOException ioEx) {
            // Wrap it
            throw new RuntimeException(ioEx);
        }
    }

    public static void close(OutputStream outputStream) {
        try {
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    public static void close(InputStream inputStream) {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
}

Related

  1. fileToString(File file, int max)
  2. fileToString(File file, String charsetName)
  3. fileToString(File file, String charsetName)
  4. fileToString(File files[])
  5. fileToString(final File file)
  6. fileToString(final String filePath)
  7. fileToString(InputStream file)
  8. fileToString(InputStream inputStream)
  9. fileToString(String file)