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

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

Description

read File As String

License

Open Source License

Declaration

public static String readFileAsString(File file, String encoding) throws IOException 

Method Source Code


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

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static String readFileAsString(File file) throws IOException {
        StringBuffer fileData = new StringBuffer(1000);
        BufferedReader reader = new BufferedReader(new FileReader(file));
        char[] buf = new char[1024];
        int numRead = 0;
        while ((numRead = reader.read(buf)) != -1) {
            fileData.append(buf, 0, numRead);
        }//w w  w .  ja  va  2  s  .c om
        reader.close();
        return fileData.toString();
    }

    public static String readFileAsString(File file, String encoding) throws IOException {
        StringBuffer fileData = new StringBuffer(1000);
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
        char[] buf = new char[1024];
        int numRead = 0;
        while ((numRead = reader.read(buf)) != -1) {
            fileData.append(buf, 0, numRead);
        }
        reader.close();
        return fileData.toString();
    }
}

Related

  1. readFileAsString(File file, String encoding)
  2. readTextFile(File f, String fileEncoding)
  3. readTextFile(File file, String encoding)
  4. readTextFile(File file, String encoding)
  5. readTextFile(File path, String encoding)