Java InputStreamReader Read readFile(String file)

Here you can find the source of readFile(String file)

Description

read File

License

LGPL

Declaration

public static String readFile(String file) throws IOException 

Method Source Code

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

import java.io.BufferedReader;

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

public class Main {
    public static String readFile(String file) throws IOException {
        return readFile(file, null);
    }/*  w w w. j av  a 2  s  . com*/

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

        File f = new File(file);

        if (!f.exists())
            return null;

        FileInputStream fis = null;
        BufferedReader br = null;

        try {

            fis = new FileInputStream(f);

            if (charset != null) {

                br = new BufferedReader(new InputStreamReader(fis, charset));

            } else {

                br = new BufferedReader(new InputStreamReader(fis));
            }

            StringBuilder sb = new StringBuilder(1024);

            String line;

            while ((line = br.readLine()) != null) {

                sb.append(line).append('\n');
            }

            return sb.toString();

        } finally {

            br.close();

        }
    }
}

Related

  1. readFile(String file)
  2. readFile(String file)
  3. readFile(String file)
  4. readFile(String file)
  5. readFile(String file)
  6. readFile(String file, String charset)
  7. readFile(String file, String lineBreak)
  8. readFile(String fileFullPath)
  9. readFile(String fileName)