Android Text File Read readTextFile(String path)

Here you can find the source of readTextFile(String path)

Description

Reads the contents of a text file into a String.

License

Open Source License

Parameter

Parameter Description
path File to read

Exception

Parameter Description
IOException in case of failure

Return

File contents

Declaration

public static String readTextFile(String path) throws IOException 

Method Source Code

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

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

public class Main {
    /**//  w  w w  .j ava2s  .c o m
     * Reads the contents of a text file into a String.
     * <p>
     * Notes: - Assumes file uses the system default encoding - Newlines are converted to
     * the system default
     * 
     * @param path File to read
     * @return File contents
     * @throws IOException in case of failure
     */
    public static String readTextFile(String path) throws IOException {
        StringBuilder contents = new StringBuilder();

        String lineSep = System.getProperty("line.separator");
        BufferedReader input = new BufferedReader(new FileReader(path));
        try {
            String line;
            while ((line = input.readLine()) != null) {
                contents.append(line);
                contents.append(lineSep);
            }
        } finally {
            input.close();
        }

        return contents.toString();
    }
}

Related

  1. readLines(String file)
  2. readStringFromClasspath(String path, Class c)
  3. readStringFromFile(String fileName)
  4. readText(String path)
  5. readTextFile(String filePath)
  6. setFileResourceText(String path, String content)
  7. getContent(String file, String encodType)
  8. getString(File file)
  9. readFileSdcard(File file)