IO基礎加強 輸入輸出流

------字節輸出流   fileOutPutStream

public static void main(String[] args) throws IOException {

//1、創建目標對象,表示要把數據存入的地方
File target = new File("c:/abc/1.txt");
//2、創建字節流輸出對象
// OutputStream fos = new FileOutputStream(target);
//創建一個可以追加字符的流,該流寫入時不會刪除之前的
OutputStream fos2 = new FileOutputStream(target,true);
//3、輸出流操作
// 輸出流的write方法
//void write(int b) 把一個字節寫入文件(此處可以理解爲byte因爲byte小於int)
// fos.write(65);
//void  write(byte[] b) 將字節數組寫進文件
// fos.write("abc".getBytes());
//void write(byte[] b, int off, int len) 把數組b中從off開始的len個字節
//從byte中第二個字符開始取2個字符
fos2.write("abc".getBytes(),1,2);
//4、關閉資源對象
fos2.close();
 

}

 

 

------字節輸入流   fileInPutStream

 

//1、創建源對象,表示要讀取的文件
File target = new File("c:/abc/1.txt");
//2、創建字節流輸入對象
InputStream is = new FileInputStream(target);
//3、讀取操作
//int read() 從文件中讀取一個字節當沒有數據時候會返回-1
int read = is.read();
//int read(byte[] b) 讀取多個字節並存入數組中(如果此時數組的length大於文件字節總數,則數組值以0補充)

//比如使用new byte[5]   去讀一個1.txt的文件   文件內容爲ABC 則讀出來爲[65,66,67,0,0] 最後兩位爲空  補0

read(byte[])方法會返回int類型,這個數字代表讀取到了多少個字節   我們獲取數據時,要根據數組中的實際字節數來

 

//int read(byte[] b, int off, int len)讀取多個字節並存入數組中
//指定從索引位置(off)開始存儲(len)個字節
byte[] b = new byte[6];
int read = is.read(b,3,2);

System.out.println(Arrays.toString(b));

 

//循環讀取所有的數據 根據 read(byte[]) 方法返回的讀取字節數判斷文件是否已經讀取完畢

byte[] b = new byte[5];
int i = -1;
while((i=is.read(b))!=-1){
System.out.println(new String(b,0,i));
}

//4、關閉資源
is.close();

}

 

------利用流來做一次文件拷貝  

public static void main(String[] args) throws IOException {
//1、創建源對象目標對象
File sourse = new File("c:/abc/w.wmv");
File target = new File("c:/abc/xxx.wmv");
//2、創建字節流輸入對象
InputStream is = new FileInputStream(sourse);
OutputStream os = new FileOutputStream(target);
//3、讀取操作
byte[] b = new byte[10];
int lenth = -1 ; //表示讀取出文件的長度
while((lenth=is.read(b))!=-1){

                    //這裏一定要制定長度,否則會有 
os.write(b, 0, lenth);
}
//4、關閉資源
is.close();
os.close();
}               

 

 

 

------流的異常處理方法

public static void main(String[] args){
InputStream is = null;
OutputStream os = null;
try {
//1、創建源對象目標對象
File sourse = new File("c:/abc/w.wmv");
File target = new File("c:/abc/xxx.wmv");
//2、創建字節流輸入對象
is = new FileInputStream(sourse);
os = new FileOutputStream(target);
//3、讀取操作
byte[] b = new byte[10];
int lenth = -1 ; //表示讀取出文件的長度
while((lenth=is.read(b))!=-1){
os.write(b);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//4、關閉資源(此時又需要處理異常)
try {
//如果流對象爲null 又會報空指針
if(is != null){
   is.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
//爲防止close方法報錯而使得os沒有關閉,所以理論上所有的流都應該單獨寫關閉方法
try {
//如果流對象爲null 又會報空指針
if(os != null){
   os.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}

}

}

 

 

自從java7開始提供了自動關閉資源的方法,比較方便實用上面一坨可以改爲如下寫法,實際上就是把try上面加了一對( )   ,在括號裏面存放需要關閉的資源  ,jvm會自動幫你關閉,這樣看起來代碼是不是簡潔多了呢?

 

public static void main(String[] args){
//1、創建源對象目標對象
File sourse = new File("c:/abc/w.wmv");
File target = new File("c:/abc/xxx.wmv");
try (
//2、創建字節流輸入對象
InputStream is = new FileInputStream(sourse);
OutputStream os = new FileOutputStream(target);
){
//3、讀取操作
byte[] b = new byte[10];
int lenth = -1 ; //表示讀取出文件的長度
while((lenth=is.read(b))!=-1){
os.write(b);
}
} catch (Exception e) {
e.printStackTrace();
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章