All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.panhongan.util.FileUtil Maven / Gradle / Ivy

There is a newer version: 0.1.8
Show newest version
package com.github.panhongan.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FileUtil {
	
	private static Logger logger = LoggerFactory.getLogger(FileUtil.class);
	
	public static List readFile(String file) {
		List list = new ArrayList();
		
		BufferedReader br = null;
		
		try {
			br = new BufferedReader(new FileReader(file));
			String line = null;
			
			while ((line = br.readLine()) != null) {
				list.add(line);
			}
		} catch (Exception e) {
			logger.warn(e.getMessage());
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (Exception e) {
					
				}
			}
		}
		
		return list;
	}
	
	public static void readFile(String file, LineProcessor processor) {
		if (processor == null) {
			logger.warn("invalid parameter : processor");
			return;
		}
		
		BufferedReader br = null;
		
		try {
			br = new BufferedReader(new FileReader(file));
			String line = null;
			
			while ((line = br.readLine()) != null) {
				processor.process(line);
			}
		} catch (Exception e) {
			logger.warn(e.getMessage());
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (Exception e) {
					
				}
			}
		}
	}
	
	public static interface LineProcessor {
		public void process(String line);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy