一個簡單易用的Java文本寫入類TextWriter

1 前言

爲了降低耦合並提供強大的功能,Java提供了非常豐富多樣的Stream類,如InputStream, OutputStream, BufferedStream等。強大的功能同樣也是一把雙刃劍,給使用記憶帶來了很多麻煩,不是每個人都是天天使用Java的這些類,並把它們記得非常清楚。相信有很多和筆者一樣的人,每次在使用的時候,都需要查看相應的代碼。爲了解決這個問題,筆者根據常用的功能,編寫了一個簡易的Java類,TextWriter,專門用於文本的讀寫。

2 功能描述

這個類的主要就是用於將文本寫入至文件中。文本寫的時候其實只需要三步,打開,寫入,關閉即可。所以對應的此類提供了相應的功能:

2.1 打開

打開是在新建的時候完成的,本類提供了三個構造函數,分別是

// 需要寫入的文件名
TextWriter(String filename)
// 需要寫入的文件名和是否爲追加模式,如果不是追加模式,且指定文件已經存在,則會清除原來存在的文件。
TextWriter(String filename, boolean append)
// 與上一個相比,增加了字符體,包括 US-ASCII,ISO-8859-1,UTF-8,UTF-16BE,UTF-16LE 和 UTF-16 
TextWriter(String filename, boolean append, String charset)

2.2 寫入

提供了兩個函數用於寫入分別如下:

// 將字符串s追加至文件中。
write(String s)
// 將字符串s追加至文件中, 同時追加換行。在Windows下追加 \r\n,在Linux系統下追加 \n
writeLine(String s)

這兩個是最基本的寫入方便,如果覺得不夠,比如 write(int v) 可以自行擴展。

2.3 關閉

每次寫完記得調用close()方法保存即可。

3 應用示例

public static void main(String[] args) throws Exception {
	String filename = "D:\\data\\text.txt";
	TextWriter tw = new TextWriter(filename, false);
	for(int i = 0; i < 10; i ++) {
		tw.writeLine("同學1 i = " + i);
	}
	tw.close();
	
	tw = new TextWriter(filename, true);
	for(int i = 0; i < 10; i ++) {
		tw.writeLine("同學2 i = " + i);
	}
	tw.close();
	System.out.println("Done.");
}

4 源代碼

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * @author 郝偉老師
 * @date 2020/02/19
 * @description 一個簡單的Java方便寫入類。
 */
public class TextWriter {

	String filename;
	String cs;  
	RandomAccessFile raf;
	String seperator;


	void setSeperetor(){
		seperator = new File("C:").exists() ? "\r\n" : "\n";
	}

	/**
	 * 向一個文本文件中添加內容,默認編碼爲UTF8.
	 * @param filepath 文件路徑。
	 */
	public TextWriter(String filepath) throws IOException {
		initialize(filename, false, "utf-8");
	}
	
	/**
	 * 向一個文本文件中添加內容,默認編碼爲UTF8.
	 * @param filepath 文件路徑。
	 * @param append 是否追加。
	 * @throws IOException
	 */
	public TextWriter(String filename, Boolean append) throws IOException{
		initialize(filename, append, "utf-8");
	} 

	/**
	 * 向一個文本文件中添加內容,默認編碼爲UTF8.
	 * @param filepath 文件路徑。
	 * @param append 是否追加。
	 * @param cs 編碼格式,可以使用StandardCharsets表示。
	 * @throws IOException
	 */
	public TextWriter(String filename, Boolean append, String charset) throws IOException{
		initialize(filename, append, charset);
	} 

	private void initialize(String filename, Boolean append, String charset) throws IOException{
		this.filename = filename;
		this.cs = charset; 
		setSeperetor(); 		
		if(append) {
			raf = new RandomAccessFile(this.filename, "rw");
			raf.seek(raf.length());
		}else{
			File file = new File(filename);
			if(file.exists()) {
				file.delete();
			}
			raf = new RandomAccessFile(this.filename, "rw");
		}
	}
	
	/**
	 * 寫入指定的字符。
	 * @param c 待寫入字符。
	 * @throws IOException
	 */
	public void write(char c) throws IOException{
		write(c + "");
	}
	
	/**
	 * 寫入指定的字符串。
	 * @param s 待寫入字符串。
	 * @throws IOException
	 */
	public void write(String s) throws IOException{
		raf.write(s == null ? new byte[0] : s.getBytes(cs));
	}
	
	/**
	 * 寫入指定的字符串,並自動追加換行符。
	 * @param s 待寫入字符串。
	 * @throws IOException
	 */
	public void writeLine(String s) throws IOException {
		write(s);
		write(seperator);
	}
	
	public void close() throws IOException {
		if(raf != null)
			raf.close();
	}	
}

發佈了323 篇原創文章 · 獲贊 91 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章