這篇文章主要介紹了java io讀取文件操作代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
主要分為字節(jié)讀取和字符讀取,字節(jié)讀取可以一個(gè)一個(gè)讀取和字節(jié)數(shù)組讀取,字符讀取同樣之,字符讀取適合文本讀取,字節(jié)讀取皆可以
這里直接上代碼,讀取文件的9個(gè)小demo
package com.io; import org.junit.Test; import java.io.*; public class FileTest { //1、字節(jié)流字節(jié)一個(gè)一個(gè)讀取 @Test public void test() throws IOException{ InputStream fis = new FileInputStream(new File("E:\project_test\src\com\io\readme.txt")); int len; //按字節(jié)一個(gè)一個(gè)讀取 while ((len = fis.read())!=-1){ System.out.print((char)len+"t"); }; fis.close(); } //輸出結(jié)果h e l l o w o r l d //2、字節(jié)流字節(jié)數(shù)組讀取 @Test public void test1() throws IOException{ InputStream fis = new FileInputStream(new File("E:\project_test\src\com\io\readme.txt")); byte[] bytes = new byte[2]; int len ; //按字節(jié)數(shù)組讀取 bytes存儲(chǔ)的是讀取的數(shù)據(jù) while ((len = fis.read(bytes))!=-1){ System.out.print((new String(bytes))+"t"); }; fis.close(); } //輸出結(jié)果 he ll ow or ld //3、緩沖字節(jié)流字節(jié)一個(gè)一個(gè)讀取 @Test public void test2() throws IOException{ BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt"))); int len ; while ((len = bis.read())!=-1){ System.out.print((char)len+"t"); }; bis.close(); } //輸出結(jié)果h e l l o w o r l d //4、緩沖字節(jié)流字節(jié)數(shù)組讀取 @Test public void test3() throws IOException{ BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt"))); byte[] bytes = new byte[3]; int len ; //按字節(jié)數(shù)組讀取 bytes存儲(chǔ)的是讀取的數(shù)據(jù) while ((len = bis.read(bytes))!=-1){ System.out.print(new String(bytes)+"t"); }; bis.close(); } //輸出結(jié)果hel low orl drl //5、字符流一個(gè)一個(gè)讀取 @Test public void test4() throws IOException{ InputStreamReader isr = new InputStreamReader(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt"))); int len ; //按字節(jié)數(shù)組讀取 bytes存儲(chǔ)的是讀取的數(shù)據(jù) while ((len = isr.read())!=-1){ System.out.print((char)len+"t"); }; isr.close(); } //6、字符流字符數(shù)組讀取 @Test public void test5() throws IOException{ InputStreamReader isr = new InputStreamReader(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt"))); char[] chars = new char[3]; int len ; //按字節(jié)數(shù)組讀取 bytes存儲(chǔ)的是讀取的數(shù)據(jù) while ((len = isr.read(chars))!=-1){ System.out.print(new String(chars)+"t"); }; isr.close(); } //7、緩沖字符流字符一個(gè)一個(gè)讀取 @Test public void test6() throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt")))); int len ; //按字節(jié)數(shù)組讀取 bytes存儲(chǔ)的是讀取的數(shù)據(jù) while ((len = br.read())!=-1){ System.out.print((char)len+"t"); }; br.close(); } //8、緩沖字符流字符數(shù)組讀取 @Test public void test7() throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt")))); char[] chars = new char[3]; int len ; //按字節(jié)數(shù)組讀取 bytes存儲(chǔ)的是讀取的數(shù)據(jù) while ((len = br.read(chars))!=-1){ System.out.print(new String(chars)+"t"); }; br.close(); } //9、緩沖字符流按行讀取 @Test public void test8() throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt")))); //按字節(jié)數(shù)組讀取 bytes存儲(chǔ)的是讀取的數(shù)據(jù) String str; while ( (str = br.readLine())!=null){ System.out.print(str+"t"); }; br.close(); } }
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站jinyejixie.com,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)站欄目:javaio讀取文件操作代碼實(shí)例-創(chuàng)新互聯(lián)
分享URL:http://jinyejixie.com/article42/heshc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、服務(wù)器托管、關(guān)鍵詞優(yōu)化、ChatGPT、全網(wǎng)營(yíng)銷(xiāo)推廣、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)