File回顧


字節流和字符流的去別:

字節流直接操作文件本身

字符流操作使用了緩衝區,關閉字符流時會強制輸出緩存區內容,否則緩衝區內容無法輸出(也可以用flush)

//FileWrite 不是 Write的子類,而是轉換流的子類 是 OutputStreamWrite的子類
//FileOutputStream 是 OutputStream 的直接子類
// out = new OutputStreamWrite(new FileOutputStream(file))
// out = new OutputStreamWrite(new FileOutputStream(file))
//不管是使用字節流還是字符流 實際上都是以字節的形式 操作輸入輸出的


內存操作流(ByteArrayInputStream)一般在生成臨時信息才用

管道流 (PipedInputStream) 主要用於線程間的通信 輸出流是管道的發送端


package com.xzl.files;


import java.io.IOException;


public class PipleInputStreamTest {
public static void main(String[] args){
PipeOutputStreamSend send = new PipeOutputStreamSend();
PipeInputStreamReceive receive = new PipeInputStreamReceive();
try {
send.getPos().connect(receive.pis());//連接管道
} catch (IOException e) {
e.printStackTrace();
}
new Thread(send).start();
new Thread(receive).start();
}
}


public class WriteTest {
public static void main(String[] args) throws Exception{
//第1步:使用file 找到文件
File file = new File("E:"+File.separator+"Test"+File.separator+"writeTest.txt");
//第2步:通過子類實例化父類對象
Writer out = null;
out = new FileWriter(file,true);
//第3步:寫操作
String str ="合肥市的房頂上";
out.write(str);
//第4步:關閉輸出流
out.close();
}
}


public class FileIO {
public static void main(String[] args) throws Exception{
/**
* IO操作的步驟
* 1.File打開一個文件
* 2.字節流 或 字符流  指定輸出位置
* 3.進行讀寫操作
* 4.關閉輸入輸出
*/
//1.通過file 找到 一個文件
File file = new File("E:"+File.separator+"Test"+File.separator+"test3.txt");
//file.createNewFile();
//2.通過子類實例化父類對象
OutputStream ops = null;//準備輸出對象流
ops = new FileOutputStream(file,true);//實例化,true表示追加內容
String str = "\r\n換行測試";
byte b[] = str.getBytes();//只能輸出byte數組,將字符串變爲byte數組
ops.write(b);
ops.close();

}
}
發佈了12 篇原創文章 · 獲贊 4 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章