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

cn.miw.utils.BigTextFileProcess Maven / Gradle / Ivy

Go to download

大文本文件读取工具(线程+缓冲区读取+回调处理),支持同时读取多个文件,一行代码搞定. 多个文件同时读取: new BigTextFileProcess().readBigFile((fileName,line) -> { System.out.println(fileName+":"+line); }, fileNames); 读取单个文件: new BigTextFileProcess().readBigFile(fileName,(fileName,line)->{System.out.println(fileName+":"+line);});

There is a newer version: 0.0.3
Show newest version
package cn.miw.utils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 大文本文件读取工具(线程+缓冲区读取+回调处理)
 * 
 * @author mrzhou
 *
 */
public class BigTextFileProcess {

	public static void main(String[] args) throws IOException {
		if (args.length > 1 && args[1] != null)
			new BigTextFileProcess().readBigFile(args[1], line -> { System.out.println(line); });
	}

	/**
	 * 读大文本文件
	 * 
	 * @author [email protected]
	 * @param fileName    文件名
	 * @param lineHandler 行处理器
	 */
	public void readBigFile(String fileName, IProcessLineHandler lineHandler) {
		new Thread(() -> {
			try {
				FileInputStream inputStream = new FileInputStream(fileName);
				InputStreamReader streamReader = new InputStreamReader(inputStream);
				BufferedReader reader = new BufferedReader(streamReader);

				String line = reader.readLine();
				while (line != null && lineHandler != null) {
					lineHandler.process(line);
					line = reader.readLine();
				}
				reader.close();
				inputStream.close();
			} catch (IOException e) {
				System.err.println("文件操作错误:" + e.getMessage());
			}
		}).start();
	}

	/**
	 * 行处理器
	 * 
	 * @author [email protected]
	 *
	 */
	public interface IProcessLineHandler {
		void process(String line);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy