Java使用I/O流讀取文件內(nèi)容的方法詳解
本文實(shí)例講述了Java使用I/O流讀取文件內(nèi)容的方法。分享給大家供大家參考,具體如下:
要利用I/O流讀取文件內(nèi)容,首先要掌握InputStream的體系結(jié)構(gòu)。
這個體系中FileInputStream和BufferedInputStream是一定要掌握的,因為使用的頻率比較高。
InputStream的方法:InputStream位于java.io包下
OutputStream的方法:
讀取文件(代碼):
package com.jredu.oopch11; 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.oopch11; 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,返回實(shí)際讀取的字節(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.oopch11; 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,返回實(shí)際讀取的字節(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.oopch11; 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.oopch11; 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.oopch11; 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è)置斷點(diǎn) 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.oopch11; 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.oopch11; 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(); } } }
更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
hibernate一對多關(guān)聯(lián)映射學(xué)習(xí)小結(jié)
這篇文章主要介紹了hibernate一對多關(guān)聯(lián)映射學(xué)習(xí)小結(jié),需要的朋友可以參考下2017-09-09java實(shí)現(xiàn)追加內(nèi)容到文件末尾的常用方法分析
這篇文章主要介紹了java實(shí)現(xiàn)追加內(nèi)容到文件末尾的常用方法,結(jié)合具體實(shí)例分析了java文件流及寫入指針等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10詳解如何配置Spring Batch批處理失敗重試機(jī)制
這篇文章主要來和大家一起探討一下如何在Spring批處理框架中配置重試邏輯,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-06-06Springboot 如何使用 SaToken 進(jìn)行登錄認(rèn)證、權(quán)限管理及路由規(guī)則接口攔截
Sa-Token 是一個輕量級 Java 權(quán)限認(rèn)證框架,主要解決:登錄認(rèn)證、權(quán)限認(rèn)證、單點(diǎn)登錄、OAuth2.0、分布式Session會話、微服務(wù)網(wǎng)關(guān)鑒權(quán) 等一系列權(quán)限相關(guān)問題,這篇文章主要介紹了Springboot 使用 SaToken 進(jìn)行登錄認(rèn)證、權(quán)限管理以及路由規(guī)則接口攔截,需要的朋友可以參考下2024-06-06