1.IO流的學習:IO流是用來處理設備之間的數據傳輸
字節流:字節流可以操作任何數據,因爲在計算機中任何數據都是以字節的形式存儲的
字節流的抽象父類:InputStream,OutputStream
字符流:字符流只能操作純字符數據,比較方便
字符流的抽象父類:Reader,Writer
2.IO程序的過程:使用前,導入IO包中的類,使用時,進行異常處理,使用後,釋放資源
3.InputStream類:是一個抽象類,不能直接new對象,同理OutputStream也一樣
FileInputStream類:從文件系統中的某個文件中獲得輸入字節
4.使用四種方式進行拷貝:
4.1 逐個字節地拷貝圖片
4.2 一次將所有字節拷貝,用available()方法,可以獲取所有字節的個數(有可能導致內存溢出)
4.3 定義小數組拷貝(拷貝的標準格式)
4.4 使用(緩衝區)BufferedInputStream和BufferedOutputStream的拷貝
5.flush和close方法的區別:
flush()方法是用來刷新緩衝區的,刷新後可以再次寫出;
close()方法是用來釋放資源的,如果是帶緩衝區的流對象的close()方法,不但會關閉流,還會在關閉流之前刷新緩衝區,關閉後不能再寫出
6.字節流讀寫中文
6.1 需要解決字節流在讀中文的時候有可能會讀到半個中文,造成亂碼的問題
6.2 字節流直接操作的字節,所以寫出中文必須將字符串轉換成字節數組
6.3 寫出回車換行 write("\r\n".getBytes());
7.流的標準處理異常代碼(使用於jdk1.6版本及其以前)
8.流的標準處理異常代碼(使用於jdk1.7版本)
package pra_16;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class J_30 {
public static void main(String[] args) throws IOException {
//1.創建字符輸入流對象
FileInputStream fis =new FileInputStream("b.txt");
int x;
while((x=fis.read())!=-1){ //從硬盤上讀取一個字節,結束的標誌是-1
System.out.println(x);
}
fis.close(); //釋放資源
//2.創建字節輸出流對象,如果沒有這個文件,會直接創建,如果有則會清除裏面的數據
FileOutputStream fos=new FileOutputStream("b.txt");
fos.write(97); //寫出的是int數,但是到文件的是一個字節會自動去除前三個八位
fos.close();
//3.FileOutputStream的追加數據
FileOutputStream fos2=new FileOutputStream("b.txt",true); //追加數據,在第二個參數傳true
fos2.write(99);
fos2.close();
//4.逐個字節地拷貝圖片
FileInputStream fis3=new FileInputStream("5.jpg");
FileOutputStream fos3=new FileOutputStream("5_01.jpg");
int z;
while((z=fis3.read())!=-1){
fos3.write(z);
}
fis3.close();
fos3.close();
//5.一次將所有字節拷貝,用available()方法,可以獲取所有字節的個數(有可能導致內存溢出)
FileInputStream fis4=new FileInputStream("5.jpg");
FileOutputStream fos4=new FileOutputStream("5_02.jpg");
byte[] arr=new byte[fis4.available()]; //創建與文件一樣大小的字節數組
fis4.read(arr); //將文件上的所有字節讀到字節數組
fos4.write(arr); //將字節數組內容寫到文件上
fis4.close();
fos4.close();
//6.定義小數組拷貝(拷貝的標準格式)
FileInputStream fis5=new FileInputStream("b.txt");
FileOutputStream fos5=new FileOutputStream("b_01.txt");
byte[] arr2=new byte[1024]; //一般取1024的整數倍
int length;
while((length=fis5.read(arr2))!=-1){ //判斷還有沒有數據
fos5.write(arr2,0,length);
}
fis5.close();
fos5.close();
//7.使用(緩衝區)BufferedInputStream和BufferedOutputStream的拷貝
FileInputStream fis6=new FileInputStream("b.txt");
FileOutputStream fos6=new FileOutputStream("b_02.txt");
BufferedInputStream bis=new BufferedInputStream(fis6); //創建輸入緩衝區對象,對輸入流進行包裝
//BufferedInputStream bis=new BufferedInputStream(new FileInputStream("b.txt"));
BufferedOutputStream bos=new BufferedOutputStream(fos6); //創建輸出緩衝區對象,對輸出流進行包裝
//BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("b_02.txt"));
int k;
while((k=bis.read())!=-1){
bos.write(k);
}
bis.close(); //只需要將緩衝區對象的流關閉即可
bos.close();
//8.字節流讀寫中文(一箇中文佔用兩個字節,所以字節流在讀中文時會出現問題,由字符流來解決)
FileInputStream fis7=new FileInputStream("k.txt");
byte[] arr4=new byte[4];
int len;
while((len=fis7.read(arr4))!=-1){
System.out.println(new String(arr4,0,len));
}
fis7.close();
FileOutputStream fos7=new FileOutputStream("k_01.txt");
fos7.write("我是一個大帥哥!".getBytes());
fos7.write("\r\n".getBytes());
fos7.write("我是一個大帥哥!".getBytes());
fos7.close();
}
}
使用於jdk1.6版本及其以前的流的標準處理異常代碼
package pra_16;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class J_31 {
/**
* @param args
*/
public static void main(String[] args) throws IOException{
FileInputStream fis=null; //這個null不能去掉
FileOutputStream fos=null;
try{
fis=new FileInputStream("k.txt");
fos=new FileOutputStream("k_02.txt");
int b;
while((b=fis.read())!=-1){
fos.write(b);
}
}finally{
//能關一個儘量關一個
try{
if(fis!=null)
fis.close();
}finally{
if(fos!=null)
fos.close();
}
}
}
}
使用於jdk1.7版本的流的標準處理異常代碼
package pra_16;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class J_32 {
public static void main(String[] args) throws IOException{
//當把流對象放到try的小括號裏頭後,再執行大括號代碼以後會自動調用流對象關閉的方法進行關閉
try(
FileInputStream fis=new FileInputStream("k.txt");//具備自動關閉的功能
FileOutputStream fos=new FileOutputStream("k_02.txt");
Mcloss mc=new Mcloss(); //已經關閉!
){
int b;
while((b=fis.read())!=-1){
fos.write(b);
}
}
}
}
class Mcloss implements AutoCloseable{
@Override
public void close(){
System.out.println("已經關閉!");
}
}