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

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

Description

read Text File

License

Open Source License

Declaration

public static StringBuffer readTextFile(File path, String encoding) 

Method Source Code

//package com.java2s;
/**//from  w w  w .jav a2  s. com
 * Copyright(c) 2005 Dragonfly - created by FengChun
 * All Rights Reserved.
 * 
 * @license: Dragonfly Common License
 * @date 2005-5-16
 */

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 StringBuffer readTextFile(File path, String encoding) {
        StringBuffer sb = new StringBuffer("");

        InputStreamReader read = null;
        BufferedReader reader = null;
        try {
            read = new InputStreamReader(new FileInputStream(path), encoding);
            reader = new BufferedReader(read);

            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (read != null)
                try {
                    read.close();
                } catch (IOException e) {
                }
            if (reader != null)
                try {
                    reader.close();
                } catch (IOException e) {
                }
        }

        return sb;
    }
}

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 file, String encoding)
  6. readTextFile(InputStream in, String encoding)
  7. readTextFile(InputStream in, String encoding)
  8. readTextFile(java.io.File file, String encoding)
  9. readTextFile(String sFileName, String sEncode)