Java InputStreamReader Read readFile(String file, String lineBreak)

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

Description

read File

License

Apache License

Declaration

public static String readFile(String file, String lineBreak) 

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);
    }// www.j a  v  a 2 s .c om

    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;
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        StringBuilder buf = new StringBuilder();
        try {
            fis = new FileInputStream(file);
            isr = new InputStreamReader(fis);
            br = new BufferedReader(isr);
            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();
                }
            }
            if (isr != null) {
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return buf.toString();
    }
}

Related

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