Android UTF8 File Read readFile(String fileName, String encoding)

Here you can find the source of readFile(String fileName, String encoding)

Description

read File

Declaration

public static String readFile(String fileName, String encoding) 

Method Source Code

//package com.java2s;
import java.io.BufferedReader;

import java.io.File;
import java.io.FileInputStream;

import java.io.InputStreamReader;
import java.nio.charset.Charset;

public class Main {

    public static String readFile(String fileName) {
        try {//w ww .  j a v a2 s  . co  m
            File adfile = new File(fileName);
            StringBuffer content = new StringBuffer();
            if (adfile.isFile() && adfile.exists()) {
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(adfile),
                        Charset.defaultCharset());
                BufferedReader in = new BufferedReader(read);
                String line1;
                while ((line1 = in.readLine()) != null) {
                    content = content.append(line1 + ";");
                }
            }

            return content.toString();
        } catch (Exception e) {
            return null;
        }

    }

    public static String readFile(String fileName, String encoding) {
        try {
            File adfile = new File(fileName);
            StringBuffer content = new StringBuffer();
            if (adfile.isFile() && adfile.exists()) {
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(adfile), encoding);
                BufferedReader in = new BufferedReader(read);
                String line1;
                while ((line1 = in.readLine()) != null) {
                    content = content.append(line1);
                }
            }

            return content.toString();
        } catch (Exception e) {
            return null;
        }
    }
}

Related

  1. loadUTF8(File file)
  2. loadUTF8LinesFromFile(File file)
  3. readFromFile(String fileName)