---
title: "解决readLine() 不区分\\r \\n的问题。"
author: "Goro"
date: "2012-08-16T14:44:23Z"
tags: []
categories: ["编程语言"]
slug: "readline-r-n"
draft: false
lastmod: "2012-08-16T16:20:19Z"
id: 1633979
url: "https://goro.iteye.com/blog/1633979"
---

csdn上碰到一个提问者希望只在\n时，才换行来读取文本文件的每一行。
但java的BufferedReader#readLine() 不区分\r 还是\n 都会换到下一行。

并且提问者的文件还特别大，不能一次全读到内存里来。
为了这个写了个自己的类。 （还没好好测过。。。有bug欢迎留言）



```java

package sh.pl;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class LineReader {

	private static final int BUFFER_SIZE = 4096;
	private Reader in;
	private int bufferSize;
	private List<String> bufferedLines;
	private String lineSeparator;
	private String preString;

	public LineReader(Reader in) {
		if (in == null) {
			throw new IllegalArgumentException("in is null");
		}
		this.in = in;
		bufferedLines = new ArrayList<String>();
		lineSeparator = System.getProperty("line.separator");
		bufferSize = BUFFER_SIZE;
	}

	public void close() throws IOException {
		in.close();
	}

	public void setBufferSize(int bufferSize) {
		if (bufferSize <= 0) {
			throw new IllegalArgumentException("bufferSize <= 0");
		}
		this.bufferSize = bufferSize;
	}

	public void setLineSeparator(String lineSeparator) {
		if (lineSeparator == null) {
			throw new IllegalArgumentException("lineSeparator is null");
		}
		this.lineSeparator = lineSeparator;
	}

	public String readLine() throws IOException {
		if (bufferedLines.size() > 1) {
			return bufferedLines.remove(0);
		} else if (bufferedLines.size() == 1) {
			preString = bufferedLines.remove(0);
			return readLine();
		} else {
			char[] src = new char[buffersize];
			int len;
			if ((len = in.read(src)) != -1) {
				String bufferString = new String(src, 0, len);
				if (preString != null) {
					bufferString = preString + bufferString;
					preString = null;
				}
				boolean returnDelims = false;
				StringTokenizer token = new StringTokenizer(bufferString,
						lineSeparator, returnDelims);
				while (token.hasMoreTokens()) {
					bufferedLines.add(token.nextToken());
				}
				if (bufferString.endsWith(lineSeparator)) {
					bufferedLines.add("");
				}
				return readLine();
			} else {
				if (preString != null) {
					String ret = preString;
					preString = null;
					return ret;
				} else {
					return null;	
				}				
			}
		}
	}
}

```


使用

```java

LineReader lr = null;
try {
	lr = new LineReader(new InputStreamReader(new FileInputStream(
			fileName), yourFileEncode));
	//指定换行符
	lr.setLineSeparator("\n");
	String line;
	while ((line = lr.readLine()) != null) {
		System.out.println("'" + line + "'");
	}
} catch (FileNotFoundException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} finally {
	if (lr != null) {
		try {
			lr.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}			
}

```