1.位元組流
1.輸入流:inputstream()
1.子類:fileinputstream()
2.輸出流:outputstream()
2.子類:fileoutputstream()
3.操作流程
1.硬碟—輸入流---->記憶體—輸出流----->硬碟
2.**示例
fileinputstream fis = new fileinputstream("aaa.txt");//建立輸入流 關聯硬碟上的aaa.txt
fileoutputstream fos = new fileoutputstream("bbb.txt");//建立輸出流 關聯硬碟上的bbb.txt
int len;
byte arr = new byte[1024*8]; //定義小陣列 加快讀寫
while((len =fis.read(arr))!=-1)
fis.close();//關流
fos.close(); //關流
}
4.帶快取區的輸入輸出流
1.bufferedinputstream
2.bufferedoutputstream
3.**實列:
bufferedinputstream bis = new bufferedinputstream(new fileinputstream("aaa.txt"));
bufferedoutputstream bos = new bufferedoutputstream(new fileoutputstream("bbb.txt"));
int len;
while((len=bis.read())!=-1)
bis.close();
bos.close();
2.字元流
1.reader–filereader 字元輸入流及其子類
1.readline 讀取一行 不含換行
2.newline 跨平台輸出"\r\n"
2.writer–filewriter 字元輸出流及其子類
3.filereader 和 filewriter 的使用**實列
filereader fr = new filereader("aaa.txt");
filewriter fw = new filewriter("bbb.txt");
int len ;
char arr = new char[1024*8];
while((len =fr.read(arr))!=-1)
fr.close();
fw.close();
4.bufferedreader 和 bufferedwriter (帶緩衝區,加快讀寫 效率略低小陣列)**示例
filereader fr = new filereader("aaa.txt");
filewriter fw = new filewriter("bbb.txt");
bufferedreader br = new bufferedreader(fr);
bufferedwriter bw = new bufferedwriter(fw);
int len ;
//char arr = new char[1024*8];
while((len =br.read())!=-1)
br.close();
bw.close();
}
5.帶指定編碼表**示例
inputstreamreader isr =
new inputstreamreader(new bufferedinputstream(new fileinputstream("aaa.txt")),"gbk");//以gbk讀取
outputstreamwriter osw =
new outputstreamwriter(new bufferedoutputstream(new fileoutputstream("bbb.txt")), "gbk");//以gbk寫入
int len ;
//char arr = new char[1024*8];
while((len =isr.read())!=-1)
isr.close();
osw.close();
序列流(sequenceinputstream:整合多個位元組流)
1.整合兩個位元組流,輸出,示例**
fileinputstream fis1 = new fileinputstream("aaa.txt");
fileinputstream fis2 = new fileinputstream("aaa.txt");
sequenceinputstream sis = new sequenceinputstream(fis1, fis2);
fileoutputstream fos = new fileoutputstream("ccc.txt");
int len ;
while((len=sis.read())!=-1)
sis.close();
fos.close();
2.整合多個位元組流,輸出,示例**
fileinputstream fis1 = new fileinputstream("aaa.txt");
fileinputstream fis2 = new fileinputstream("bbb.txt");
fileinputstream fis3 = new fileinputstream("ccc.txt");
fileoutputstream fos = new fileoutputstream("ddd.txt");
vectorvt= new vector<>();//建立集合
vt.add(fis1);
vt.add(fis2);
vt.add(fis3);
enumerationen = vt.elements();//獲取列舉的引用
sequenceinputstream sis = new sequenceinputstream(en);
int len;
while((len=sis.read())!=-1)
sis.close();
fos.close();
java基礎之io流
1.四大抽象基類 位元組流 inputstream outputstream 字元流 writer reader 位元組輸出流寫檔案用其子類fileoutputstream類 構造 fileoutputstream file file 傳遞file物件包裝檔案 string name 傳遞字串型別檔...
java基礎之IO流
io流概念 輸入流 把能夠讀取乙個位元組序列的物件稱為輸入流。輸出流 把能夠寫乙個位元組序列的物件稱為輸出流。通俗理解 對於初學者,可能常常不清楚何時該用輸入流,何時該用輸出流。本人將這兩個流記為 讀入寫出 那麼我就清楚輸入流就有read 讀 方法,輸出流就有write 寫 方法。然後,再思考 讀入...
java基礎筆記之IO流之字元流
知識點 字元流 是直接讀取字元的io流 字元輸入流 reader 讀的時候,是把位元組轉成字元,然後再讀取 filereader 字元輸入流 bufferedreader 特有方法 public string readline 一次讀取一行,讀不到返回null,讀到就返回對應的資料 string型別...