IO流
IO流通常用來處理設(shè)備之間的數(shù)據(jù)傳輸
Java對數(shù)據(jù)的操作是通過流的形式
Java用于操作的流的類都在IO包
按照流向:分為輸入流和輸出流
按操作類型:
字節(jié)流:字節(jié)流可以操作任何數(shù)據(jù)离斩,因為計算機(jī)的數(shù)據(jù)都是按照字節(jié)存放
字符流:字符流只能操作純字符數(shù)據(jù),比較方便
字符大 字節(jié)小
常用的父類:
抽象父類:InputStream(字節(jié)輸入流)? OutputStream(字節(jié)輸出流)
抽象父類: Reader(字符輸入流) Writer(字符輸出流)
IO程序書寫
導(dǎo)入IO包類,進(jìn)行IO異常處理,釋放資源
--------------------------------------------------------------------------
FileInputStream
FileInputStream fis = new FileInputStream("xxx.txt");
int x = fis.read();
System.out.println(x);
int y = fis.read();
System.out.println(y);
fis.close();
每read()一次,就向后移動一次并再次讀取
注意,文件的結(jié)束標(biāo)記是-1
while((len = fis.read())!=-1){
System.out.println(len);
}
fis.close();
為什么返回Int而不是Byte
任何文件讀取的結(jié)束都是-1 為了保證讀取過程中不會因為讀到byte的-1而終止讀取過程,因此選擇讀取int來執(zhí)行操作
-----------------------------------------------------------------------------
拷貝圖片哀墓,音樂 逐字節(jié)拷貝特別慢
available()方法 獲取讀取文件的所有字節(jié)個數(shù)(這個方法可能導(dǎo)致內(nèi)存溢出)
FileInputStream fis = new FileInputStream("xxx.txt");
byte[] arr = new byte[fis.available()];//創(chuàng)建和文件一樣大小的字節(jié)數(shù)組
fis.read(arr); 將文件上的字節(jié)讀取到文件中(字節(jié)數(shù)組中)
FileOutputStream fos = new FileOutputStream("xxx");
fos.write(arr); 將字節(jié)數(shù)組中的字節(jié)數(shù)據(jù)寫到文件上
----------------------------------------------------------------------------
讀取小數(shù)組的方法
FileInputStream fis = new FileInputStream("xxx.txt");
byte[] arr = new byte[1024];
int len = 0;
while((len = fis.read(arr))!=-1)}{
fos.write(arr,0,len);
如果忘記加arr 返回不是讀取的字節(jié)個數(shù),而是字節(jié)的碼表值喷兼,一定要先讀到字節(jié)數(shù)里
-----------------------------------------------------------------------------
BufferInputStream
FileInputStream fis = new FileInputStream("xxx.txt");
FileOutputStream fos = new FileOutputStream("copy.txt");
BufferedInputStream bis = new BufferedInputStream(fis);//創(chuàng)建緩沖區(qū)篮绰,對輸入流進(jìn)行包裝
BufferedOutputStream bos = new BufferedOutputStream(fos);//創(chuàng)建緩沖區(qū),對輸出流進(jìn)行包裝
int b ;
while((b=bis.read())!=-1){
bos.write(b);
}
bis.close();
bos.close();
字節(jié)流一次讀取一個數(shù)組速度要更快
裝飾設(shè)計模式褒搔,提供了字節(jié)緩沖區(qū)流
BuffferedInputStream bis 先在文件讀取8192個數(shù)送到緩沖區(qū)阶牍,然后程序去緩沖區(qū)讀取喷面,如果所有緩沖區(qū)數(shù)據(jù)都被讀完,再去獲取8192個數(shù)據(jù)
BufferedOutputStream bos 程序?qū)?shù)據(jù)送到緩沖區(qū)走孽,然后將緩沖區(qū)寫滿時惧辈,在將數(shù)據(jù)一次性送入到文件中
相比較而言,定義小數(shù)組要比Buffered讀取更快磕瓷,因為其讀和寫都是同一個數(shù)組盒齿,而Buffered操作的是兩個數(shù)組
-----------------------------------------------------------------------------
flush()和close()的區(qū)別
BufferedInputStream bis? = new BufferedInputStream(new FileInputStream());
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream());
如果不加close 拷貝文件會稍微小一點點 close方法具備刷新的功能,關(guān)閉流之前就會刷新一次緩沖區(qū)困食,將緩沖區(qū)的字節(jié)全部刷新到文件上边翁,再關(guān)閉
用bos.flush()直接刷新緩沖區(qū)即可
close和flush相比較, flush刷新完仍然可以繼續(xù)寫入
flush--->實時刷新
--------------------------------------------------------------------------
字節(jié)流讀寫中文(字節(jié)流讀取中文造成亂碼)(GBK中一個字符兩個字節(jié))
FileInputStream fis = new FileInputStream("xxx.txt");
byte[] arr = new byte[3]();
int len;
while((len = fis.read())!=-1){
? new String(arr,0,len);
}
fis.close();
而用字節(jié)流寫出中文
FileOutputStream fos = new FileOutputStream("xxx");
fos.write("xxx".getBytes());
fos.write("\r\n".getBytes());//回車換行
fos.close();
--------------------------------------------------------------------------
標(biāo)準(zhǔn)異常處理代碼
TryFinally
JDK 1.6以前
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream("xxx.txt");
fos = new FileOutputStream("xxx.txt");
}catch(Exception e){
e.printStackTrace();
}
finally{
try{
if(fis != null){
fis.close();
}
}finally{
if(fos != null){
fos.close();
}
}
JDK1.7
try(
FileInputStream fis = new FileInputStream("xxx.txt");
FileOutputStream fos = new FileOutputStream("xxx.txt");
){
int b;
while ((b = fis.read()) != -1) {
fos.write(b);
}
}
在try的小括號中書寫類硕盹,必須要實現(xiàn)AutoCloseale接口
------------------------------------------------------
加密用異或操作符匾,一個數(shù)異或兩次等于其本身
拷貝文件
注意如果是自己拷貝文件,/r/n是換行操作符