Java FileInputStream Read readFileToStr(String fn, String charset)

Here you can find the source of readFileToStr(String fn, String charset)

Description

read File To Str

License

Apache License

Declaration

public static String readFileToStr(String fn, String charset) 

Method Source Code

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

import java.io.FileInputStream;

public class Main {
    public static String readFileToStr(String fn) {
        return readFileToStr(fn, null);
    }/*w w  w  .  ja  va2  s. c o m*/

    public static String readFileToStr(String fn, String charset) {
        FileInputStream in = null;
        try {
            in = new FileInputStream(fn);
            try {
                byte[] buf = new byte[in.available()];
                int r = in.read(buf);
                return charset == null ? new String(buf, 0, r) : new String(buf, 0, r, charset);
            } finally {
                in.close();
            }
        } catch (Exception e) {
            // e.printStackTrace();
        }
        return "";
    }
}

Related

  1. readFileToByteArray(String file)
  2. readFileToByteArray(String filename)
  3. readFileToBytes(File f)
  4. readFileToBytes(File f, int max)
  5. readFileToBytes(String path)
  6. readFileUTF8(String file)
  7. readFileUTF_8(String filePath)
  8. writeFile(File file, byte[] buffer, ZipOutputStream zos)
  9. writeFile(InputStream fileInputStream, OutputStream outputStream)