Java BufferedReader Read All readAll(String filePath)

Here you can find the source of readAll(String filePath)

Description

read All

License

Apache License

Declaration

public static String readAll(String filePath) 

Method Source Code


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

import java.io.*;

public class Main {
    public static String readAll(String filePath) {

        try {/*  w  ww  .ja v  a 2s .c  o  m*/
            FileReader fr = new FileReader(filePath);
            BufferedReader br = new BufferedReader(fr);
            String res = "";
            String buf;
            while ((buf = br.readLine()) != null) {
                res += buf;
            }
            br.close();
            fr.close();
            return res;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String readAll(String path, String charset) {

        try {
            File file = new File(path);

            if (!file.exists()) {
                return null;
            }

            FileInputStream inputStream = new FileInputStream(file);
            byte[] length = new byte[inputStream.available()];
            inputStream.read(length);
            inputStream.close();
            return new String(length, charset);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}

Related

  1. readAll(Reader reader)
  2. readAll(Reader reader)
  3. readAll(Reader reader)
  4. readAll(String fileName)
  5. readAll(String fileName)
  6. readAll(String filePathAndName)
  7. readAll(String ip)
  8. readAll(String path)
  9. ReadAll_Variant2(Reader rd, Writer wr)