這篇文章主要介紹了java io讀取文件操作代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
創(chuàng)新互聯(lián)建站于2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術服務公司,擁有項目網(wǎng)站設計、成都做網(wǎng)站網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元博州做網(wǎng)站,已為上家服務,為博州各地企業(yè)和個人服務,聯(lián)系電話:18980820575主要分為字節(jié)讀取和字符讀取,字節(jié)讀取可以一個一個讀取和字節(jié)數(shù)組讀取,字符讀取同樣之,字符讀取適合文本讀取,字節(jié)讀取皆可以
這里直接上代碼,讀取文件的9個小demo
package com.io; import org.junit.Test; import java.io.*; public class FileTest { //1、字節(jié)流字節(jié)一個一個讀取 @Test public void test() throws IOException{ InputStream fis = new FileInputStream(new File("E:\project_test\src\com\io\readme.txt")); int len; //按字節(jié)一個一個讀取 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存儲的是讀取的數(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é)一個一個讀取 @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存儲的是讀取的數(shù)據(jù) while ((len = bis.read(bytes))!=-1){ System.out.print(new String(bytes)+"t"); }; bis.close(); } //輸出結(jié)果hel low orl drl //5、字符流一個一個讀取 @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存儲的是讀取的數(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存儲的是讀取的數(shù)據(jù) while ((len = isr.read(chars))!=-1){ System.out.print(new String(chars)+"t"); }; isr.close(); } //7、緩沖字符流字符一個一個讀取 @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存儲的是讀取的數(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存儲的是讀取的數(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存儲的是讀取的數(shù)據(jù) String str; while ( (str = br.readLine())!=null){ System.out.print(str+"t"); }; br.close(); } }
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)建站muchs.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
網(wǎng)站欄目:javaio讀取文件操作代碼實例-創(chuàng)新互聯(lián)
分享URL:http://muchs.cn/article42/heshc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、服務器托管、關鍵詞優(yōu)化、ChatGPT、全網(wǎng)營銷推廣、面包屑導航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)