您好,登錄后才能下訂單哦!
這篇文章主要介紹了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(); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。