Java中的字符型文件流FileReader和FileWriter詳細(xì)解讀
字符型文件流
與字節(jié)型文件流不同,字節(jié)型文件流讀取和寫入的都是一個(gè)又一個(gè)的字節(jié)。
而字符型文件流操作的單位是一個(gè)又一個(gè)的字符,字符型流認(rèn)為一個(gè)字母是一個(gè)字符,而一個(gè)漢字也是一個(gè)字符。
字符型文件流一般只能夠用來操作一些文本格式的文件,即可以用記事本正常打開的文件。 (如:.txt .java .c .properties .html .js .xml)
字符型文件流解決了使用字節(jié)型文件流讀寫純文本文件時(shí)可能發(fā)生的中文亂碼問題,所以讀寫純文本文件是字符型文件流的強(qiáng)項(xiàng)。
它的用法與字節(jié)型文件流(FileInputStream,F(xiàn)ileOutputStream)基本一致,只不過它每次讀寫的數(shù)組類型是char[]而不是byte[]。
我們說所有帶Reader字眼的都是字符型流。
而同時(shí)帶Stream和Reader字眼的它們是字節(jié)字符轉(zhuǎn)換流。
FileReader
繼承關(guān)系:
常用構(gòu)造方法:
- FileReader(File file)
- FileReader(String fileName)
常用方法:
常用的4個(gè)
- read(char[] chars)
- read(char[] cbuf,int off,int len)
- skip(long n)
- close()
簡(jiǎn)單嘗試: 準(zhǔn)備一個(gè).txt文件:
讀取它:
public static void main(String[] args) { File file = new File("F:\\test\\Test.txt"); FileReader fr = null; try { fr = new FileReader(file); char[] chars = new char[10];//每次讀取5個(gè)字符 int count = fr.read(chars); while (count!=-1){ String s = new String(chars, 0, count); System.out.println(s); count = fr.read(chars); } } catch (IOException e) { e.printStackTrace(); }finally { if (fr!=null){ try { fr.close(); } catch (IOException e) { e.printStackTrace(); } } } }
執(zhí)行結(jié)果:
換行是我打印的時(shí)候加上去的(每讀取10個(gè)字符打印一次)。
FileWriter
繼承關(guān)系:
常用構(gòu)造方法:
- FileWriter(File file)
- FileWriter(File file, boolean append)
- FileWriter(String fileName)
- FileWriter(String fileName, boolean append)
常用方法:
常用的6個(gè)
- write(char[] cbuf)
- write(String str)
- write(String str,int off,int len)
- write(String cbuf,int off,int len)
- close()
- flush()
簡(jiǎn)單嘗試:
public static void main(String[] args) { File file = new File("F:\\test\\log.txt"); //log.txt可以不存在,test文件夾必須存在 FileWriter fw = null; try { fw = new FileWriter(file,true);//要追加寫入數(shù)據(jù) String str = "文字是人類用表義符號(hào)記錄表達(dá)信息,使之傳之久遠(yuǎn)的方式和工具?,F(xiàn)代文字大多是記錄語(yǔ)言的工具。人類往往先有口頭的語(yǔ)言后產(chǎn)生書面文字,很多方言和小語(yǔ)種,有語(yǔ)言但沒有文字。文字的不同體現(xiàn)了國(guó)家和民族的書面表達(dá)的方式和思維不同。文字使人類進(jìn)入有歷史記錄的文明社會(huì)。\n" + "Text is a way and tool for human beings to record and express information by means of symbolic meaning.Modern writing is mostly a tool for recording language.Humans tend to have spoken languages before they have written languages, many dialects and smaller languages that have languages but no words.The different characters reflect the different ways and thinking of the written expression of the country and nation.Writing enabled mankind to enter a civilization with a historical record."; char[] chars = str.toCharArray(); fw.write(chars); } catch (IOException e) { e.printStackTrace(); }finally { if (fw!=null){ try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } } }
執(zhí)行結(jié)果:
到此這篇關(guān)于Java中的字符型文件流FileReader和FileWriter詳細(xì)解讀的文章就介紹到這了,更多相關(guān)Java的字符型文件流內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用Java實(shí)現(xiàn)簡(jiǎn)單ATM機(jī)功能
這篇文章主要為大家詳細(xì)介紹了用Java實(shí)現(xiàn)簡(jiǎn)單ATM機(jī)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01MyBatis-plus更新對(duì)象時(shí)將字段值更新為null的實(shí)現(xiàn)方式
mybatis-plus在執(zhí)行更新操作,當(dāng)更新字段為 空字符串 或者 null 的則不會(huì)執(zhí)行更新,如果要將指定字段更新null,可以通過以下三種方式實(shí)現(xiàn),感興趣的小伙伴跟著小編一起來看看吧2023-10-10在lambda的foreach遍歷中break退出操作(lambda foreach break)
這篇文章主要介紹了在lambda的foreach遍歷中break退出操作(lambda foreach break),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09生產(chǎn)環(huán)境jvm常用的參數(shù)設(shè)置建議分享
在Java應(yīng)用程序的部署過程中,合理配置JVM(Java虛擬機(jī))參數(shù)對(duì)于提升應(yīng)用性能、穩(wěn)定性和資源利用效率至關(guān)重要,本文將探討一些常用的JVM參數(shù)設(shè)置建議,幫助開發(fā)者在生產(chǎn)環(huán)境中優(yōu)化Java應(yīng)用,需要的朋友可以參考下2025-04-04