Java Text File Read by Encode getFileContentAsString(File file, String encoding)

Here you can find the source of getFileContentAsString(File file, String encoding)

Description

Copies the contents of the given file to a string.

License

BSD License

Parameter

Parameter Description
file the contents of this file are copied to a string
encoding the character encoding of the file contents

Exception

Parameter Description
FileNotFoundException an exception
IOException an exception

Return

contents of the file as a string

Declaration

public static String getFileContentAsString(File file, String encoding)
        throws FileNotFoundException, IOException 

Method Source Code


//package com.java2s;
/*//  ww  w .j  av  a2s  .c  om
 [The "BSD licence"]
 Copyright (c) 2015, John Snyders
 All rights reserved.
    
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:
 1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
 2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
 3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
    
 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import java.io.*;

public class Main {
    protected static final int BLKSIZE = 8192;

    /**
     * Copies the contents of the given file to a string.
     * @param file the contents of this file are copied to a string
     * @param encoding the character encoding of the file contents
     * @return contents of the file as a string
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static String getFileContentAsString(File file, String encoding)
            throws FileNotFoundException, IOException {
        FileInputStream fis = new FileInputStream(file);
        return getStreamAsString(fis, encoding);
    }

    /**
     * Copies the contents of the given stream to a string.
     * @param is the stream to copy to a string
     * @param encoding the character encoding of the input stream
     * @return contents of the stream as a string
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static String getStreamAsString(InputStream is, String encoding)
            throws FileNotFoundException, IOException {
        Reader r = null;
        if (encoding == null) {
            r = new InputStreamReader(is);
        } else {
            r = new InputStreamReader(is, encoding);
        }
        try {
            StringWriter w = new StringWriter();
            char[] buffer = new char[BLKSIZE];

            int len;
            while ((len = r.read(buffer)) != -1) {
                w.write(buffer, 0, len);
            }

            return w.toString();
        } finally {
            r.close();
        }
    }
}

Related

  1. getFileContent(File file, String charset)
  2. getFileContent(File file, String charsetName)
  3. getFileContent(FileInputStream fis, String encoding)
  4. getFileContent(InputStream is, String encoding)
  5. getFileContentAsString(File file, String encoding)
  6. getFileContentAsString(File file, String encoding)
  7. getFileContentAsString(String filePath, String fileEncoding)
  8. getFileContents(InputStream stream, String encoding)