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();
            }
        }
  • 相关阅读:
    mysql 性能优化方案1
    微信接口php
    IT girl
    jsonp 使用总结
    Oracle 10g体系机构及安全管理《思维导图》
    图形化报表
    JQuery中Ajax应用
    jquery中的事件与动画
    jquery的DOM操作
    jQuery中的选择器《思维导图》
  • 原文地址:https://www.cnblogs.com/pypua/p/9991668.html
Copyright © 2011-2022 走看看