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();
        }
    }

 

相关文章:

  • 2022-12-23
  • 2021-10-28
  • 2022-02-07
  • 2022-12-23
  • 2021-09-25
  • 2022-01-31
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-22
  • 2021-07-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案