Android Text File Read readFileLineByLine(File file)

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

Description

Reads file and returns contents as ArrayDeque, where each element is a line of the file

Parameter

Parameter Description
file java.io.File object of file to read

Exception

Parameter Description
FileNotFoundException thrown if file specified does not exist

Return

File contents as String, or "" if file is blank

Declaration

public static ArrayDeque<String> readFileLineByLine(File file)
        throws FileNotFoundException 

Method Source Code

//package com.java2s;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;

public class Main {
    /**//from  w  w w . j ava 2s .  c  o m
     * Reads file and returns contents as ArrayDeque<String>, where each element is a line of the file
     * @param file java.io.File object of file to read
     * @return File contents as String, or "" if file is blank
     * @throws FileNotFoundException thrown if file specified does not exist
     */
    public static ArrayDeque<String> readFileLineByLine(File file)
            throws FileNotFoundException {

        //Setup
        String filePath = file.getAbsolutePath();
        FileInputStream fstream = new FileInputStream(filePath);
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        ArrayDeque<String> forReturn = new ArrayDeque<String>();

        try {
            // Read File Line By Line
            while ((strLine = br.readLine()) != null) {
                // Print the content on the console
                forReturn.add(strLine.trim());
            }

            // Close the input stream
            in.close();
        } catch (IOException e) {
            //do nothing, file is presumably empty so return blank ArrayDeque<String>         
        }

        return forReturn;

    }
}

Related

  1. readFile(String filename)
  2. readFile(String path)
  3. readFileAsString(File file)
  4. readFileAsString(File file, String charset)
  5. readFileAsString(String filename)
  6. readFree(File file)
  7. readFromFile(File file)
  8. readFull(String path)
  9. readLines(String file)