Java使用I/O流讀取文件內(nèi)容的方法詳解
本文實(shí)例講述了Java使用I/O流讀取文件內(nèi)容的方法。分享給大家供大家參考,具體如下:

要利用I/O流讀取文件內(nèi)容,首先要掌握InputStream的體系結(jié)構(gòu)。

這個(gè)體系中FileInputStream和BufferedInputStream是一定要掌握的,因?yàn)槭褂玫念l率比較高。
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ù)流向某個(gè)對(duì)象的數(shù)據(jù)序列,并且到達(dá)這個(gè)對(duì)象的過(guò)程。
* 輸入流:數(shù)據(jù)源數(shù)據(jù)流向計(jì)算機(jī)內(nèi)存的過(guò)程
* 輸出流:把數(shù)據(jù)從程序流向目標(biāo)數(shù)據(jù)源的過(guò)程
* @author Administrator
*
*/
public class Ch01 {
/**
* 讀取文件內(nèi)容
* @param args
*/
public static void main(String[] args) {
//InputStream:是一個(gè)抽象類
// \:是一個(gè) 轉(zhuǎn)移符
//表示磁盤路徑的兩種表示方式:1、\\ 2、/
try {
//從文件地址中讀取內(nèi)容到程序中
//1、建立連接
InputStream is = new FileInputStream("E:/iodemo/ch01.txt");
//2、開始讀取信息
/*
//方法1:一次只讀一個(gè)
System.out.println(is.read());//讀取的是字節(jié)型的:49
System.out.println((byte)is.read());//50
*/
//方法2:定義數(shù)組,循環(huán)讀取
//先定義一個(gè)字節(jié)數(shù)組存放數(shù)據(jù)
byte[] b = new byte[5];//把所有的數(shù)據(jù)讀取到這個(gè)字節(jié)當(dāng)中
//聲明一個(gè)int存儲(chǔ)每次讀取到的數(shù)據(jù)
int i = 0;
//定義一個(gè)記錄索引的變量
int index = 0;
//循環(huán)讀取每個(gè)數(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)制解決的問(wèn)題:文件沒(méi)有找到
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ù)流向某個(gè)對(duì)象的數(shù)據(jù)序列,并且到達(dá)這個(gè)對(duì)象的過(guò)程。
* 輸入流:數(shù)據(jù)源數(shù)據(jù)流向計(jì)算機(jī)內(nèi)存的過(guò)程
* 輸出流:把數(shù)據(jù)從程序流向目標(biāo)數(shù)據(jù)源的過(guò)程
* @author Administrator
*
*/
public class Ch02 {
/**
* 讀取文件內(nèi)容
* @param args
*/
public static void main(String[] args) {
//InputStream:是一個(gè)抽象類
// \:是一個(gè) 轉(zhuǎn)移符
//表示磁盤路徑的兩種表示方式:1、\\ 2、/
try {
//從文件地址中讀取內(nèi)容到程序中
//1、建立連接
InputStream is = new FileInputStream("E:/iodemo/ch01.txt");
//2、開始讀取信息
//先定義一個(gè)字節(jié)數(shù)組存放數(shù)據(jù)
byte[] b = new byte[5];//把所有的數(shù)據(jù)讀取到這個(gè)字節(jié)當(dāng)中
//完整的讀取一個(gè)文件
is.read(b);
//read:返回的是讀取的文件大小
//最大不超過(guò)b.length,返回實(shí)際讀取的字節(jié)個(gè)數(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)制解決的問(wèn)題:文件沒(méi)有找到
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ù)流向某個(gè)對(duì)象的數(shù)據(jù)序列,并且到達(dá)這個(gè)對(duì)象的過(guò)程。
* 輸入流:數(shù)據(jù)源數(shù)據(jù)流向計(jì)算機(jī)內(nèi)存的過(guò)程
* 輸出流:把數(shù)據(jù)從程序流向目標(biāo)數(shù)據(jù)源的過(guò)程
* @author Administrator
*
*/
public class Ch03 {
/**
* 讀取文件內(nèi)容
* @param args
*/
public static void main(String[] args) {
//InputStream:是一個(gè)抽象類
// \:是一個(gè) 轉(zhuǎn)移符
//表示磁盤路徑的兩種表示方式:1、\\ 2、/
try {
//從文件地址中讀取內(nèi)容到程序中
//1、建立連接
InputStream is = new FileInputStream("E:/iodemo/ch01.txt");
//2、開始讀取信息
//先定義一個(gè)字節(jié)數(shù)組存放數(shù)據(jù)
byte[] b = new byte[is.available()];//把所有的數(shù)據(jù)讀取到這個(gè)字節(jié)當(dāng)中
//is.available():返回文件的大小
// while(is.available()==0);//不等于0時(shí)才停止循環(huán)
//完整的讀取一個(gè)文件
int off = 0;
int le = 2;
while(is.read(b, off, 2)!=-1){
off+=1;
}
is.read(b,off,2);
//read:返回的是讀取的文件大小
//最大不超過(guò)b.length,返回實(shí)際讀取的字節(jié)個(gè)數(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)制解決的問(wèn)題:文件沒(méi)有找到
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():文件的長(zhǎng)度
byte[] b=new byte[fis.available()];
//skip:跳過(guò)n個(gè)字節(jié)后再開始讀取
fis.skip(5);//跳過(guò)前5個(gè)
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 {
/**
* 讀取過(guò)程暫停,給當(dāng)前做一個(gè)標(biāo)記,下一次從標(biāo)記位置開始讀取
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//讀取過(guò)程中暫停
//給當(dāng)前做一個(gè)標(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));//位置就是讀取的長(zhǎng)度
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個(gè)流合并在一起讀取
* @author Administrator
*
*/
public class Ch07 {
public static void main(String[] args) {
try {
//第一個(gè)文件流
FileInputStream fis1=new FileInputStream("E:/iodemo/ch01.txt");
//第二個(gè)文件流
FileInputStream fis2=new FileInputStream("E:/iodemo/ch04.txt");
//合并到序列流中
SequenceInputStream sis=new SequenceInputStream(fis1, fis2);
//方式1
// //臨時(shí)存放數(shù)據(jù)的數(shù)組
// int len =fis1.available()+fis2.available();
// byte[] b=new byte[2*len+1];
// //把每一次讀取到的臨時(shí)數(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 {
//三個(gè)文件流
FileInputStream fis1 = new FileInputStream("E:/iodemo/a.txt");
FileInputStream fis2 = new FileInputStream("E:/iodemo/b.txt");
FileInputStream fis3 = new FileInputStream("E:/iodemo/c.txt");
//把三個(gè)流添加到集合中
Vector<FileInputStream> vector = new Vector<>();
vector.add(fis1);
vector.add(fis2);
vector.add(fis3);
// vector.elements(); //方法返回的是Enumeration
//合并到一個(gè)序列流中
SequenceInputStream sis = new SequenceInputStream(vector.elements());
byte[] b = new byte[fis1.available()+fis2.available()+fis3.available()];
//讀取
int off=0;
//vector.get(i).available():一個(gè)文件的長(zhǎng)度
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());//每次讀取一個(gè)文件的長(zhǎng)度
}
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緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
springboot使用jasypt對(duì)配置文件加密加密數(shù)據(jù)庫(kù)連接的操作代碼
這篇文章主要介紹了springboot使用jasypt對(duì)配置文件加密加密數(shù)據(jù)庫(kù)連接的操作代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01
使用SpringBoot_jar方式啟動(dòng)并配置日志文件
這篇文章主要介紹了使用SpringBoot_jar方式啟動(dòng)并配置日志文件操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Mybatis如何實(shí)現(xiàn)關(guān)聯(lián)屬性懶加載
這篇文章主要介紹了Mybatis如何實(shí)現(xiàn)關(guān)聯(lián)屬性懶加載的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Spring BeanPostProcessor接口使用詳解
本篇文章主要介紹了Spring BeanPostProcessor接口使用詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01

