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();
            }
        }
  • 相关阅读:
    21--数据库优化
    20--mysql读写分离,分库分表
    18--mysql主从复制、及架构
    17--数据快速导出、导入,数据库迁移
    16--mysql数据备份
    15--mysql日志管理
    14--mysql锁机制
    13--mysql事务详解,数据库读现象
    etcd原理详解代码剖析
    k8s入坑之路(10)kubernetes coredns详解
  • 原文地址:https://www.cnblogs.com/pypua/p/9991668.html
Copyright © 2011-2022 走看看