zoukankan      html  css  js  c++  java
  • java读取.txt文件工具类FileUtiles

    public class FileUtils {
    
            private static final String ENCODING = "UTF-8";//编码方式
    
            /**
             * 获取文件的行
             *
             * @param fileName
             *            文件名称
             * @return List<String>
             */
            public static String getContentByLine(String fileName) {
                StringBuffer lines = new StringBuffer();
                InputStreamReader read = null;
                BufferedReader bufferedReader = null;
                try {
                    String configPath = FileUtils.class.getClassLoader().getResource(fileName).getPath();
                    configPath = configPath.replaceAll("%20", " ");// 处理文件路径中空格问题
                    File file = new File(configPath);
                    if (file.isFile() && file.exists()) { // 判断文件是否存在
                        read = new InputStreamReader(new FileInputStream(file), ENCODING);
                        bufferedReader = new BufferedReader(read);
                        String lineTxt = null;
                        while ((lineTxt = bufferedReader.readLine()) != null) {
                            if (lineTxt == null || lineTxt.length() == 0) {
                                continue;
                            }
                            lines.append(lineTxt);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (read != null) {
                            read.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (bufferedReader != null) {
                            try {
                                bufferedReader.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
                return lines.toString();
            }
        }
  • 相关阅读:
    python中列表排序的方法
    pyrhon3中字符串方法
    python数据探索与数据与清洗概述
    2020年日期表-python实现
    python中字符串离散化的例子
    python中常见的日期处理方法
    如何简单地理解Python中的if __name__ == '__main__'
    我的老爸老了
    关于
    关于
  • 原文地址:https://www.cnblogs.com/pypua/p/9991668.html
Copyright © 2011-2022 走看看