這篇文章主要介紹Java如何使用I/O流讀取文件內(nèi)容的方法,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
成都創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設(shè)計、網(wǎng)站制作與策劃設(shè)計,濉溪網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:濉溪等地區(qū)。濉溪做網(wǎng)站價格咨詢:18980820575
具體如下:
要利用I/O流讀取文件內(nèi)容,首先要掌握InputStream的體系結(jié)構(gòu)。
這個體系中FileInputStream和BufferedInputStream是一定要掌握的,因為使用的頻率比較高。
InputStream的方法:InputStream位于java.io包下
OutputStream的方法:
讀取文件(代碼):
package com.jredu.oopch21; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; /** * I/O流的概念:數(shù)據(jù)流向某個對象的數(shù)據(jù)序列,并且到達(dá)這個對象的過程。 * 輸入流:數(shù)據(jù)源數(shù)據(jù)流向計算機(jī)內(nèi)存的過程 * 輸出流:把數(shù)據(jù)從程序流向目標(biāo)數(shù)據(jù)源的過程 * @author Administrator * */ public class Ch01 { /** * 讀取文件內(nèi)容 * @param args */ public static void main(String[] args) { //InputStream:是一個抽象類 // \:是一個 轉(zhuǎn)移符 //表示磁盤路徑的兩種表示方式:1、\\ 2、/ try { //從文件地址中讀取內(nèi)容到程序中 //1、建立連接 InputStream is = new FileInputStream("E:/iodemo/ch01.txt"); //2、開始讀取信息 /* //方法1:一次只讀一個 System.out.println(is.read());//讀取的是字節(jié)型的:49 System.out.println((byte)is.read());//50 */ //方法2:定義數(shù)組,循環(huán)讀取 //先定義一個字節(jié)數(shù)組存放數(shù)據(jù) byte[] b = new byte[5];//把所有的數(shù)據(jù)讀取到這個字節(jié)當(dāng)中 //聲明一個int存儲每次讀取到的數(shù)據(jù) int i = 0; //定義一個記錄索引的變量 int index = 0; //循環(huán)讀取每個數(shù)據(jù) while((i=is.read())!=-1){//把讀取的數(shù)據(jù)放到i中 b[index]=(byte) i; index++; } //把字節(jié)數(shù)組轉(zhuǎn)成字符串 System.out.println(new String(b)); //關(guān)閉流 is.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block //系統(tǒng)強(qiáng)制解決的問題:文件沒有找到 e.printStackTrace(); } catch (IOException e) { //文件讀寫異常 // TODO Auto-generated catch block e.printStackTrace(); } } }
package com.jredu.oopch21; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; /** * I/O流的概念:數(shù)據(jù)流向某個對象的數(shù)據(jù)序列,并且到達(dá)這個對象的過程。 * 輸入流:數(shù)據(jù)源數(shù)據(jù)流向計算機(jī)內(nèi)存的過程 * 輸出流:把數(shù)據(jù)從程序流向目標(biāo)數(shù)據(jù)源的過程 * @author Administrator * */ public class Ch02 { /** * 讀取文件內(nèi)容 * @param args */ public static void main(String[] args) { //InputStream:是一個抽象類 // \:是一個 轉(zhuǎn)移符 //表示磁盤路徑的兩種表示方式:1、\\ 2、/ try { //從文件地址中讀取內(nèi)容到程序中 //1、建立連接 InputStream is = new FileInputStream("E:/iodemo/ch01.txt"); //2、開始讀取信息 //先定義一個字節(jié)數(shù)組存放數(shù)據(jù) byte[] b = new byte[5];//把所有的數(shù)據(jù)讀取到這個字節(jié)當(dāng)中 //完整的讀取一個文件 is.read(b); //read:返回的是讀取的文件大小 //最大不超過b.length,返回實際讀取的字節(jié)個數(shù) System.out.println(Arrays.toString(b));//讀取的是字節(jié)數(shù)組 //把字節(jié)數(shù)組轉(zhuǎn)成字符串 System.out.println(new String(b)); //關(guān)閉流 is.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block //系統(tǒng)強(qiáng)制解決的問題:文件沒有找到 e.printStackTrace(); } catch (IOException e) { //文件讀寫異常 // TODO Auto-generated catch block e.printStackTrace(); } } }
package com.jredu.oopch21; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; /** * I/O流的概念:數(shù)據(jù)流向某個對象的數(shù)據(jù)序列,并且到達(dá)這個對象的過程。 * 輸入流:數(shù)據(jù)源數(shù)據(jù)流向計算機(jī)內(nèi)存的過程 * 輸出流:把數(shù)據(jù)從程序流向目標(biāo)數(shù)據(jù)源的過程 * @author Administrator * */ public class Ch03 { /** * 讀取文件內(nèi)容 * @param args */ public static void main(String[] args) { //InputStream:是一個抽象類 // \:是一個 轉(zhuǎn)移符 //表示磁盤路徑的兩種表示方式:1、\\ 2、/ try { //從文件地址中讀取內(nèi)容到程序中 //1、建立連接 InputStream is = new FileInputStream("E:/iodemo/ch01.txt"); //2、開始讀取信息 //先定義一個字節(jié)數(shù)組存放數(shù)據(jù) byte[] b = new byte[is.available()];//把所有的數(shù)據(jù)讀取到這個字節(jié)當(dāng)中 //is.available():返回文件的大小 // while(is.available()==0);//不等于0時才停止循環(huán) //完整的讀取一個文件 int off = 0; int le = 2; while(is.read(b, off, 2)!=-1){ off+=1; } is.read(b,off,2); //read:返回的是讀取的文件大小 //最大不超過b.length,返回實際讀取的字節(jié)個數(shù) System.out.println(Arrays.toString(b));//讀取的是字節(jié)數(shù)組 //把字節(jié)數(shù)組轉(zhuǎn)成字符串 System.out.println(new String(b)); //關(guān)閉流 is.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block //系統(tǒng)強(qiáng)制解決的問題:文件沒有找到 e.printStackTrace(); } catch (IOException e) { //文件讀寫異常 // TODO Auto-generated catch block e.printStackTrace(); } } }
package com.jredu.oopch21; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Arrays; public class Ch04 { /** * 讀取中文字符的文件 * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub try { /*FileInputStream fis = new FileInputStream("E:/iodemo/ch04.txt"); //包裝流 BufferedInputStream bis = new BufferedInputStream(fis);*/ //包裝流 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:/iodemo/ch04.txt")); //讀取文件內(nèi)容 byte[] b = new byte[bis.available()]; bis.read(b); /*char[] c = new char[b.length]; for (int i = 0; i < c.length; i++) { c[i]=(char) b[i]; } System.out.println(Arrays.toString(c));//亂碼 */ System.out.println(Arrays.toString(b));//得到的是字節(jié) //String(byte[])把字節(jié)數(shù)組轉(zhuǎn)成字符串 System.out.println(new String(b));//可以得到中文 bis.close();//關(guān)閉流(關(guān)閉bis就可以了) } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package com.jredu.oopch21; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class Ch05 { public static void main(String[] args) { // TODO Auto-generated method stub try { //讀取文件 FileInputStream fis = new FileInputStream("E:/iodemo/ch01.txt"); //fis.available():文件的長度 byte[] b=new byte[fis.available()]; //skip:跳過n個字節(jié)后再開始讀取 fis.skip(5);//跳過前5個 fis.read(b); System.out.println(new String(b)); fis.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package com.jredu.oopch21; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class Ch06 { /** * 讀取過程暫停,給當(dāng)前做一個標(biāo)記,下一次從標(biāo)記位置開始讀取 * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub //讀取過程中暫停 //給當(dāng)前做一個標(biāo)記 //下一次從標(biāo)記位置開始讀取 try { BufferedInputStream bis= new BufferedInputStream(new FileInputStream("E:/iodemo/ch06.txt")); byte[] b = new byte[bis.available()]; // bis.read(b, 0, b.length/2); //設(shè)置斷點 bis.mark(bis.read(b, 0, b.length/2));//位置就是讀取的長度 System.out.println(new String(b)); System.out.println("暫停讀取...."); Thread.sleep(2000);//休眠2s //休眠后繼續(xù)讀 System.out.println("繼續(xù)讀取..."); //reset:將當(dāng)前復(fù)位的位置設(shè)置成上次調(diào)用mark標(biāo)記的位置 bis.reset(); bis.read(b); System.out.println(new String(b)); bis.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package com.jredu.oopch21; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.SequenceInputStream; /** * 序列流(集合流) * 把n個流合并在一起讀取 * @author Administrator * */ public class Ch07 { public static void main(String[] args) { try { //第一個文件流 FileInputStream fis1=new FileInputStream("E:/iodemo/ch01.txt"); //第二個文件流 FileInputStream fis2=new FileInputStream("E:/iodemo/ch04.txt"); //合并到序列流中 SequenceInputStream sis=new SequenceInputStream(fis1, fis2); //方式1 // //臨時存放數(shù)據(jù)的數(shù)組 // int len =fis1.available()+fis2.available(); // byte[] b=new byte[2*len+1]; // //把每一次讀取到的臨時數(shù)據(jù)存放如sb中 //// StringBuffer sb=new StringBuffer(); // //一次性讀取所有的內(nèi)容 // int off=0; // int i=0; // while((i=sis.read(b,off,len))!=-1) { //// sb.append(); // off+=i; // } // System.out.println(new String(b)); //方式2 byte[] b=new byte[fis1.available()]; // StringBuffer sb=new StringBuffer(); // int i=0; while(sis.read(b)!=-1) { System.out.println(new String(b)); // sb.append(new String(b)); } // System.out.println(sb.toString()); sis.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package com.jredu.oopch21; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.SequenceInputStream; import java.util.Vector; public class Ch08 { public static void main(String[] args) { // TODO Auto-generated method stub try { //三個文件流 FileInputStream fis1 = new FileInputStream("E:/iodemo/a.txt"); FileInputStream fis2 = new FileInputStream("E:/iodemo/b.txt"); FileInputStream fis3 = new FileInputStream("E:/iodemo/c.txt"); //把三個流添加到集合中 Vector<FileInputStream> vector = new Vector<>(); vector.add(fis1); vector.add(fis2); vector.add(fis3); // vector.elements(); //方法返回的是Enumeration //合并到一個序列流中 SequenceInputStream sis = new SequenceInputStream(vector.elements()); byte[] b = new byte[fis1.available()+fis2.available()+fis3.available()]; //讀取 int off=0; //vector.get(i).available():一個文件的長度 for (int i = 0; i < vector.size(); i++) { //off:數(shù)組當(dāng)中存放數(shù)據(jù)的起始下標(biāo)的位置 off+=sis.read(b, off, vector.get(i).available());//每次讀取一個文件的長度 } System.out.println(new String(b)); sis.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
以上是“Java如何使用I/O流讀取文件內(nèi)容的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)頁標(biāo)題:Java如何使用I/O流讀取文件內(nèi)容的方法
本文網(wǎng)址:http://jinyejixie.com/article12/ggscgc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、微信小程序、做網(wǎng)站、ChatGPT、小程序開發(fā)、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)