package org.algorithm.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * sql文件阅读器，生成sql语句
 * 
 * @Author: bijl
 * @Date: 2018年7月12日-上午10:00:52
 * @Description:
 */
public class TextFileReader {
    public static List<String> readLines(String filePath) {

        List<String> lines = new ArrayList<>();
        InputStreamReader read = null;
        BufferedReader bufferedReader = null;
        try {
            File file = new File(filePath);
            if (file.isFile() && file.exists()) {
                read = new InputStreamReader(new FileInputStream(file), "UTF-8");
                bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    lineTxt = lineTxt.trim();
                    lines.add(lineTxt);
                }

                bufferedReader.close();
                read.close();
                
            } else {
                throw new RuntimeException("文件不存在"); // 读取文件错误则强制抛出异常，使中断
            }

        } catch (Exception e) {
            throw new RuntimeException("读取文件错误:" + e.getMessage()); // 读取文件错误则强制抛出异常，使中断
        }

        return lines;
    }
    
}
