Android Text File Read readFile(File file)

Here you can find the source of readFile(File file)

Description

Reads the specified file and returns the content as a String.

Parameter

Parameter Description
file a parameter

Exception

Parameter Description
IOException thrown if an I/O error occurs opening the file

Declaration

public static String readFile(File file) throws IOException 

Method Source Code

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

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;

public class Main {
    /**/*  w  ww.  ja v  a2  s . c  o m*/
     * The character set. UTF-8 works good for windows, mac and Umlaute.
     */
    private static final Charset CHARSET = Charset.forName("UTF-8");

    /**
     * Reads the specified file and returns the content as a String.
     * 
     * @param file
     * @return
     * @throws IOException thrown if an I/O error occurs opening the file
     */
    public static String readFile(File file) throws IOException {
        StringBuffer stringBuffer = new StringBuffer();

        BufferedReader reader = Files.newBufferedReader(file.toPath(),
                CHARSET);

        String line = null;
        while ((line = reader.readLine()) != null) {
            stringBuffer.append(line);
        }

        reader.close();

        return stringBuffer.toString();
    }
}

Related

  1. read(String pathName)
  2. readCipherSuites(String file)
  3. readClasspathTextFile(String filename)
  4. readFile(File file)
  5. readFile(File file)
  6. readFile(File file, String encoding)
  7. readFile(File in)
  8. readFile(String fileName)
  9. readFile(String fileName)