Java tutorial
//package com.java2s; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class Main { /** * read file to string list, a element of list is a line * * @param filePath * @param charsetName The name of a supported {@link java.nio.charset.Charset </code>charset<code>} * @return if file not exist, return null, else return content of file * @throws RuntimeException if an error occurs while operator BufferedReader */ public static List<String> readFileToList(String filePath, String charsetName) { File file = new File(filePath); List<String> fileContent = new ArrayList<String>(); if (file == null || !file.isFile()) { return null; } BufferedReader reader = null; try { InputStreamReader is = new InputStreamReader(new FileInputStream(file), charsetName); reader = new BufferedReader(is); String line = null; while ((line = reader.readLine()) != null) { fileContent.add(line); } reader.close(); return fileContent; } catch (IOException e) { throw new RuntimeException("IOException occurred. ", e); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { throw new RuntimeException("IOException occurred. ", e); } } } } }