Java InputStreamReader Read readFile(File file)

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

Description

read File

License

Apache License

Declaration

public static String readFile(File file) 

Method Source Code


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

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) {
        return readFile(file, null);
    }//  w w  w.  ja  v a  2  s .  com

    public static String readFile(String file, String lineBreak) {
        File f = new File(file);
        return readFile(f, lineBreak);
    }

    public static String readFile(File file) {
        return readFile(file, null);
    }

    public static String readFile(File file, String lineBreak) {
        String lb = lineBreak == null ? "\r\n" : lineBreak;
        BufferedReader br = null;
        StringBuilder buf = new StringBuilder();
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            String line = null;
            while ((line = br.readLine()) != null) {
                buf.append(line).append(lb);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return buf.toString();
    }
}

Related

  1. readFile(File f)
  2. readFile(File f)
  3. readFile(File f)
  4. readFile(File f)
  5. readFile(File file)
  6. readFile(File file)
  7. readFile(File file)
  8. readFile(File file)
  9. readFile(File file)