Java Text File Read by Encoding readTextFile(File file, String encoding)

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

Description

read Text File

License

Open Source License

Declaration

public static String readTextFile(File file, String encoding) 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);
    }/*from ww w .  j  a  v  a  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. readFileAsString(File file, String encoding)
  2. readFileAsString(File file, String encoding)
  3. readTextFile(File f, String fileEncoding)
  4. readTextFile(File file, String encoding)
  5. readTextFile(File path, String encoding)
  6. readTextFile(InputStream in, String encoding)
  7. readTextFile(InputStream in, String encoding)
  8. readTextFile(java.io.File file, String encoding)