Java FileReader Create readTextFile(File file)

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

Description

read Text File

License

Open Source License

Declaration

public static String readTextFile(File file) throws FileNotFoundException, IOException 

Method Source Code


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

import java.io.*;

public class Main {
    public static String readTextFile(File file) throws FileNotFoundException, IOException {
        return readTextFile(file, null);
    }/*ww w .  j a  va 2 s .c  o  m*/

    public static String readTextFile(File file, String encoding) throws FileNotFoundException, IOException {
        BufferedReader br;
        if (encoding == null) {
            br = new BufferedReader(new FileReader(file));
        } else if (!encoding.isEmpty()) {
            br = new BufferedReader(new FileReader(file));
        } else {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
        }
        try {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();
            while (line != null) {
                sb.append(line);
                sb.append(String.format("%s%n", line));
                line = br.readLine();
            }
            return sb.toString();

        } finally {
            br.close();
        }
    }
}

Related

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