Java利用字符流輕松處理文本數(shù)據(jù)
前言
在Java中,文本數(shù)據(jù)是經(jīng)常處理的一種數(shù)據(jù)類型,而字符流就是用來處理文本數(shù)據(jù)的一種流。
相比于字節(jié)流,字符流可以更方便地處理文本數(shù)據(jù),尤其是在處理中文文本數(shù)據(jù)時,字符流的優(yōu)勢更加明顯。
在本文中,將介紹Java字符流的基本概念、常用類和方法,以及如何使用字符流來讀寫文件。
摘要
本文將會介紹以下內(nèi)容:
- Java字符流的概念和作用
- 字符流的常用類和方法
- 如何使用字符流來讀寫文件
- 應(yīng)用示例和測試用例
Java字符流的概念和作用
在Java中,字符流是用來處理文本數(shù)據(jù)的一種流。它是一種高級的流,能夠更加方便地處理文本數(shù)據(jù),特別是在處理中文文本數(shù)據(jù)時,字符流的優(yōu)勢更加明顯。
字符流將流中的字節(jié)數(shù)據(jù)按照指定的編碼方式(如UTF-8、GBK等)轉(zhuǎn)換成字符數(shù)據(jù),從而方便地對文本數(shù)據(jù)進行讀寫操作。
字符流主要包括兩種類型:字符輸入流和字符輸出流。
字符輸入流用于從文件或其他數(shù)據(jù)源中讀取字符數(shù)據(jù),常用的字符輸入流類有FileReader和InputStreamReader。
字符輸出流用于將字符數(shù)據(jù)寫入文件或其他數(shù)據(jù)目標(biāo),常用的字符輸出流類有FileWriter和OutputStreamWriter。
字符流的常用類和方法
字符輸入流
FileReader類
FileReader類是Java中最基本的字符輸入流類之一,它用于從文件中讀取字符數(shù)據(jù)。
FileReader類的構(gòu)造方法如下:
public FileReader(String fileName) throws FileNotFoundException public FileReader(File file) throws FileNotFoundException
FileReader類的常用方法如下:
public int read() throws IOException public int read(char[] cbuf) throws IOException public int read(char[] cbuf, int off, int len) throws IOException public void close() throws IOException
其中,read()方法用于讀取一個字符,read(char[] cbuf)方法用于讀取一個字符數(shù)組,read(char[] cbuf, int off, int len)方法用于讀取指定長度的字符數(shù)組,close()方法用于關(guān)閉輸入流。
InputStreamReader類
InputStreamReader類是Java中另一個常用的字符輸入流類,它能夠?qū)⒆止?jié)輸入流轉(zhuǎn)換為字符輸入流。
InputStreamReader類的構(gòu)造方法如下:
public InputStreamReader(InputStream in) public InputStreamReader(InputStream in, String charsetName)
InputStreamReader類的常用方法和FileReader類相似,這里不再贅述。
字符輸出流
FileWriter類
FileWriter類是Java中最基本的字符輸出流類之一,它用于向文件中寫入字符數(shù)據(jù)。
FileWriter類的構(gòu)造方法如下:
public FileWriter(String fileName) throws IOException public FileWriter(String fileName, boolean append) throws IOException public FileWriter(File file) throws IOException public FileWriter(File file, boolean append) throws IOException
FileWriter類的常用方法如下:
public void write(int c) throws IOException public void write(char[] cbuf) throws IOException public void write(char[] cbuf, int off, int len) throws IOException public void write(String str) throws IOException public void write(String str, int off, int len) throws IOException public void flush() throws IOException public void close() throws IOException
其中,write()方法用于寫入字符數(shù)據(jù),flush()方法用于刷新緩沖區(qū),close()方法用于關(guān)閉輸出流。
OutputStreamWriter類
OutputStreamWriter類是Java中另一個常用的字符輸出流類,它能夠?qū)⒆止?jié)輸出流轉(zhuǎn)換為字符輸出流。
OutputStreamWriter類的構(gòu)造方法如下:
public OutputStreamWriter(OutputStream out) public OutputStreamWriter(OutputStream out, String charsetName)
OutputStreamWriter類的常用方法和FileWriter類相似,這里不再贅述。
如何使用字符流來讀寫文件
使用字符流來讀寫文件的步驟如下:
- 創(chuàng)建字符輸入流對象或字符輸出流對象,根據(jù)需求選擇FileReader、InputStreamReader、FileWriter、OutputStreamWriter等類。
- 調(diào)用流對象的讀或?qū)懛椒?,對文件進行讀寫操作。
- 關(guān)閉流對象。
下面是一個使用字符流讀取文件的示例代碼:
public static void main(String[] args) { try { FileReader reader = new FileReader("test.txt"); int ch; while ((ch = reader.read()) != -1) { System.out.print((char) ch); } reader.close(); } catch (IOException e) { e.printStackTrace(); } }
測試截圖如下:
下面是一個使用字符流寫入文件的示例代碼:
public static void main(String[] args) { try { FileWriter writer = new FileWriter("test.txt"); writer.write("Hello, world!"); writer.close(); } catch (IOException e) { e.printStackTrace(); } }
測試執(zhí)行截圖如下:
應(yīng)用示例和測試用例
下面是一個使用字符流讀取文件并計算文件中字符數(shù)量的示例代碼:
@Test public void testFileReaderAndCount() { try { FileReader reader = new FileReader("test.txt"); int count = 0; int ch; while ((ch = reader.read()) != -1) { count++; } reader.close(); System.out.println("文件中共有 " + count + " 個字符。"); } catch (IOException e) { e.printStackTrace(); } }
測試用例執(zhí)行結(jié)果如下:
讀取 Hello, world!
內(nèi)容確實是13個字符。
下面是一個使用字符流寫入文件再讀取的測試用例代碼:
@Test public void testWriteAndReadFile() { try { FileWriter writer = new FileWriter("test.txt"); writer.write("goodbye!my friends"); writer.close(); FileReader reader = new FileReader("test.txt"); int ch; while ((ch = reader.read()) != -1) { System.out.print((char) ch); } reader.close(); } catch (IOException e) { e.printStackTrace(); } }
測試用例執(zhí)行結(jié)果如下:
全文小結(jié)
本文主要介紹了Java字符流的概念、常用類和方法,以及如何使用字符流來讀寫文件。
字符流是用來處理文本數(shù)據(jù)的一種流,能夠更方便地處理文本數(shù)據(jù),特別是在處理中文文本數(shù)據(jù)時,字符流的優(yōu)勢更加明顯。
在使用字符流進行文件讀寫時,需要注意關(guān)閉流對象,以保證文件資源能夠正確地釋放。
到此這篇關(guān)于Java利用字符流輕松處理文本數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Java字符流內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
maven插件spring-boot-starter-tomcat的使用方式
這篇文章主要介紹了maven插件spring-boot-starter-tomcat的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07SpringBoot?項目打成?jar后加載外部配置文件的操作方法
這篇文章主要介紹了SpringBoot?項目打成?jar后加載外部配置文件的操作方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03JavaAgent實現(xiàn)http接口發(fā)布方式淺析
這篇文章主要介紹了JavaAgent實現(xiàn)http接口發(fā)布方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-03-03學(xué)習(xí)不同 Java.net 語言中類似的函數(shù)結(jié)構(gòu)
這篇文章主要介紹了學(xué)習(xí)不同 Java.net 語言中類似的函數(shù)結(jié)構(gòu),函數(shù)式編程語言包含多個系列的常見函數(shù)。但開發(fā)人員有時很難在語言之間進行切換,因為熟悉的函數(shù)具有不熟悉的名稱。函數(shù)式語言傾向于基于函數(shù)范例來命名這些常見函數(shù)。,需要的朋友可以參考下2019-06-06Java并發(fā)編程service層處理并發(fā)事務(wù)加鎖可能會無效問題
這篇文章主要介紹了Java并發(fā)編程service層處理并發(fā)事務(wù)加鎖可能會無效問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07