Android Text File Read readFile(String fileName)

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

Description

read File

Declaration

public static String readFile(String fileName) 

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 {/*from ww w .  ja va  2 s  .c  o 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. readFile(File file)
  2. readFile(File file)
  3. readFile(File file)
  4. readFile(File file, String encoding)
  5. readFile(File in)
  6. readFile(String fileName)
  7. readFile(String filePath)
  8. readFile(String filename)
  9. readFile(String path)