Java InputStreamReader Read readFile(String path, String charset)

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

Description

read File

License

Open Source License

Declaration

public static String readFile(String path, String charset) throws IOException 

Method Source Code


//package com.java2s;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static String readFile(String path) throws IOException {
        return readFile(path, "UTF-8");
    }//from w w  w .j  a  v a  2s.  c o m

    public static String readFile(String path, String charset) throws IOException {

        FileInputStream fileStream = new FileInputStream(path);
        InputStreamReader inputStream = new InputStreamReader(fileStream, charset);

        try {
            StringBuilder sb = new StringBuilder();

            int c;
            while ((c = inputStream.read()) != -1) {
                sb.append((char) c);
            }

            return sb.toString();
        } finally {
            inputStream.close();
            fileStream.close();
        }
    }
}

Related

  1. readFile(String path)
  2. readFile(String Path)
  3. readFile(String path)
  4. readFile(String path)
  5. readFile(String path)
  6. readFile(ZipInputStream zin)
  7. readFileAddLine(String filePath, Object object)
  8. readFileAll(File file, String charsetName)
  9. readFileAsArray(String path)