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) throws Exception 

Method Source Code

//package com.java2s;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Main {
    public static String readFile(String fileName) throws Exception {
        File file = new File(fileName);
        StringBuffer contents = new StringBuffer();
        BufferedReader reader = null;

        try {//w  ww.  ja  v a  2s  .c  om
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            // repeat until all lines is read
            while ((text = reader.readLine()) != null) {
                contents.append(text).append(
                        System.getProperty("line.separator"));
            }
        } catch (FileNotFoundException e) {
            throw new Exception(e);
        } catch (IOException e) {
            throw new Exception(e);
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
            }
        }
        return contents.toString();
    }
}

Related

  1. readFile(File file)
  2. readFile(File file)
  3. readFile(File file, String encoding)
  4. readFile(File in)
  5. readFile(String fileName)
  6. readFile(String filePath)
  7. readFile(String filename)
  8. readFile(String path)
  9. readFileAsString(File file)