Java Text File Read by Encode getFileContent(InputStream is, String encoding)

Here you can find the source of getFileContent(InputStream is, String encoding)

Description

get File Content

License

Apache License

Declaration

public static String getFileContent(InputStream is, String encoding)
        throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.*;

public class Main {
    private final static String DEFAULT_CHARSET = "UTF-8";

    public static String getFileContent(String filePath) throws IOException {
        return getFileContent(filePath, DEFAULT_CHARSET);
    }/*from  ww  w . j a va2  s.c  o  m*/

    public static String getFileContent(String filePath, String encoding)
            throws IOException {
        BufferedReader buff = new BufferedReader(new InputStreamReader(
                new FileInputStream(filePath), encoding));
        String content = getContent(buff);
        buff.close();
        return content;
    }

    public static String getFileContent(File file) throws IOException {
        return getFileContent(new FileInputStream(file));
    }

    public static String getFileContent(InputStream is) throws IOException {
        BufferedReader buff = new BufferedReader(new InputStreamReader(is,
                "UTF-8"));
        String content = getContent(buff);
        buff.close();
        return content;
    }

    public static String getFileContent(InputStream is, String encoding)
            throws IOException {
        BufferedReader buff = new BufferedReader(new InputStreamReader(is,
                encoding));
        String content = getContent(buff);
        buff.close();
        return content;
    }

    private static String getContent(BufferedReader buff)
            throws IOException {
        String line;
        StringBuffer content = new StringBuffer();
        while ((line = buff.readLine()) != null) {
            content.append('\n');
            content.append(line);
        }
        return content.substring(1).toString();
    }
}

Related

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