Java Text File Read by Charset readFile(String path, Charset encoding)

Here you can find the source of readFile(String path, Charset encoding)

Description

Read a file from path and convert it into string.

License

Open Source License

Parameter

Parameter Description
path path to the file.
encoding encoding to use.

Exception

Parameter Description
IOException an exception

Return

A string version of the input file.

Declaration

@SuppressWarnings("resource")
public static String readFile(String path, Charset encoding) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Scanner;

public class Main {
    /**/*from  w w w  .j  a  va  2  s.c om*/
     * Read a file from path and convert it into string. (Method #1)
     * 
     * @param path path to the file.
     * @param encoding encoding to use.
     * @return A string version of the input file.
     * @throws IOException
     */
    @SuppressWarnings("resource")
    public static String readFile(String path, Charset encoding) throws IOException {
        return new Scanner(new File(path), "UTF-8").useDelimiter("\\A").next();
    }
}

Related

  1. readFile(File file, Charset cs)
  2. readFile(File file, String charsetName)
  3. readFile(File theFile, Charset theCharset)
  4. readFile(final InputStream is, final Charset encoding)
  5. readFile(String path, Charset charset)
  6. readFileAsString(String path, String charsetName)
  7. readFileContents(File file, Charset charset)
  8. readFileToLines(String inFile, String inCharset)
  9. readFileToList(String filePath, String charsetName)