Java BufferedReader Read readFile(String file, Boolean deleteOnExit)

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

Description

read File

License

Open Source License

Declaration

public static String readFile(String file, Boolean deleteOnExit) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import java.util.function.BiFunction;

public class Main {
    private static BiFunction<String, Boolean, String> fileReader = (file, deleteOnExit) -> {
        if (file == null) {
            return null;
        }/*from   ww w .  j  a  v a 2  s.  c  o m*/

        File rerunFile = new File(file);

        if (deleteOnExit) {
            rerunFile.deleteOnExit();
        }

        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        try {
            fileReader = new FileReader(rerunFile);
            bufferedReader = new BufferedReader(fileReader);

            StringBuilder fileContents = new StringBuilder();
            bufferedReader.lines().forEach(fileContents::append);
            return fileContents.toString();
        } catch (Exception ignored) {
            return null;
        } finally {
            try {
                if (fileReader != null)
                    fileReader.close();

                if (bufferedReader != null)
                    bufferedReader.close();
            } catch (Exception ignored) {
            }
        }
    };

    public static String readFile(String file, Boolean deleteOnExit) {
        return fileReader.apply(file, deleteOnExit);
    }
}

Related

  1. readFile(String file)
  2. readFile(String file)
  3. readFile(String file)
  4. readFile(String file)
  5. readFile(String file)
  6. readFile(String fileDir)
  7. readFile(String filename)
  8. readFile(String filename)
  9. readfile(String filename)