Here you can find the source of readFileLineByLine(File file)
Parameter | Description |
---|---|
file | java.io.File object of file to read |
Parameter | Description |
---|---|
FileNotFoundException | thrown if file specified does not exist |
public static ArrayDeque<String> readFileLineByLine(File file) throws FileNotFoundException
//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; } }