An easy to use Java text line by line reading class TextReader

1 Introduction

In order to reduce coupling and provides a powerful, Java provides a very rich and diverse Stream class, such as InputStream, OutputStream, BufferedStream and so on. Powerful is also a double-edged sword, remember to use a lot of trouble, not everyone is using these Java classes every day, and I remember them very well. I believe a lot and I like people, every time when in use, will need to see the corresponding code. To solve this problem, the author according to commonly used functions, write a simple Java class, TextReader rows of text devoted to reading.

2 Functional Description

The main line is read by the class file. Files divided into three steps read, open, read, close it.

2.1 Open

Open time is newly completed, this class provides two constructors, respectively,

// 需要写入的文件名
TextReader(String filename)
// 与上一个相比,增加了字符体,包括 US-ASCII,ISO-8859-1,UTF-8,UTF-16BE,UTF-16LE 和 UTF-16 
TextReader(String filename, String charset)

2.2 reading

Read rows only provides a unique function readLine(). While providing a variable EOFfor judging whether the end of the file.

2.3 Close

Every reading is completed remember to call close () method to close open files.

Application Example 3

	public static void main(String[] args) throws Exception {
		TextReader tr = new TextReader("D:\\data\\text.txt");
		while (!tr.EOF) {
			System.out.println(tr.readLine());
		}
		tr.close();
	}

Source Code 4

package utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;

/**
 * 日期:2020/02/21
 * 作者:郝伟老师
 * 描述:提供了按行读取一个文本文件的函数,同时支持不同编码格式。
 * 
 */
public class TextReader {
	public boolean EOF;
	String filename;
	String cs;
	RandomAccessFile raf;
	String seperator;
	BufferedReader reader = null;
	String line = null;

	/**
	 * 从一个文本文件中读取内容,默认编码为UTF8.
	 * 
	 * @param filepath 文件路径。
	 * @throws IOException
	 */
	public TextReader(String filename) throws IOException {
		initialize(filename, null);
	}

	/**
	 * 从一个文本文件中读取内容。
	 * 
	 * @param filepath 文件路径。
	 * @param cs 编码格式,可以使用StandardCharsets表示。
	 * @throws IOException
	 */
	public TextReader(String filename, String charset) throws IOException {
		initialize(filename, charset);
	}

	/**
	 * 初始化函数。
	 * @param filename 待打开的文件名。
	 * @param charset 文件集,默认为utf-8
	 * @throws IOException
	 */
	private void initialize(String filename, String charset) throws IOException {
		this.filename = filename;
		this.cs = charset == null || charset.length() == 0 ? "utf-8" : charset;
		this.reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename), cs));
		this.line = reader.readLine();
		this.EOF = line == null; 
	}

	/**
	 * 读取文件中的一行。
	 * @return
	 * @throws IOException
	 */
	public String readLine() throws IOException {
		String s = line;
		line = reader.readLine();
		EOF = line == null;
		return s;
	}

	/**
	 * 关闭当前文件。
	 * @throws IOException
	 */
	public void close() throws IOException {
		reader.close();
	}

	/**
	* 读取指定文件的所有内容。
    * @param filename 待打开的文件名。
	* @param charset 文件集,默认为utf-8
	*/
	public static String readAllLines(String filename, String charset) throws IOException{
		StringBuilder sb = new StringBuilder();
		TextReader tr = new TextReader(filename, charset);
		while (!tr.EOF) {
			sb.append(tr.readLine());
			sb.append("\r\n"); // Linux 可改成 '\n'
		}
		tr.close();
		return sb.toString();
	}
}
Published 324 original articles · won praise 91 · views 180 000 +

Guess you like

Origin blog.csdn.net/weixin_43145361/article/details/104434456