欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【工具】文件处理

程序员文章站 2024-01-14 10:22:16
...
/**
     * @Description 按行读文件,每行加入List集合
     * @Param String
     * @return List<String>
     **/
	public static List<String> readFileByLines(String filename) {
		List<String> result = null;
		BufferedReader bufferedReader = null;
		ClassPathResource resource = new ClassPathResource(filename);
		if (resource.exists()) {
			try {
				result = new ArrayList<>();
				InputStream fileInputStream = resource.getInputStream();
				bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
				String line;
				while ((line = bufferedReader.readLine()) != null) {
					result.add(line);
				}
			} catch (IOException e) {
				log.warn("readFileByLines() - io error !");
			} finally {
				if (bufferedReader != null) {
					try {
						bufferedReader.close();
					} catch (IOException e) {
						log.warn("readFileByLines() - close reader error !");
					}
				}
			}
		} else {
			log.warn("readFileByLines() - file is null, filename = " + filename);
		}
		return result;
	}
相关标签: # 工具类集合