Here you can find the source of fileToString(final File file, final String charsetName)
Parameter | Description |
---|---|
string | a parameter |
outputFileName | a parameter |
public static String fileToString(final File file, final String charsetName)
//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); } } }