大家好,樂字節(jié)小樂又來了辉懒。上一篇給大家?guī)淼氖牵?a target="_blank">Java中的IO流之輸入流|樂字節(jié)清女,本文將繼續(xù)講述IO流之輸出流钱烟。
一、輸出流
1校仑、抽象類:OutputStream 和 Writer
OutputStream和Writer也非常相似忠售。
在OutputStream 里包含如下方法:
在 Writer 中, 因為字符流直接以字符作為操作單位,所以 Writer 可以用字符串來代替字符數(shù)組迄沫,即以String對象來作為參數(shù)稻扬。 包含如下方法:
2、文件節(jié)點類: FileOutputStream 和 FileWriter
FileOutputStream 和 FileWriter羊瘩,它們都是節(jié)點流泰佳,直接和指定文件關(guān)聯(lián)。
public class WriteFile {
public static void main(String[] args) {
//1尘吗、建立聯(lián)系? File對象? 源頭 目的地
File dest=new File("c:/IO/print.txt");
//2逝她、選擇流? ? 文件輸出流? OutputStream FileOutputStream
OutputStream out=null;
//以追加形式寫出文件? 必須為true 否則會覆蓋
try {
out=new FileOutputStream(dest,true);
//3、操作
String str="shsxt is very good \r\n good good good";
//字符串轉(zhuǎn)成字節(jié)數(shù)組
byte[] data=str.getBytes();
out.write(data,0,data.length);
out.flush();//強制刷新出去
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("文件未找到");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("文件寫出失敗");
}finally{
try {
if(out!=null){
out.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("關(guān)閉輸出流失敗");
}
}
}
}
//1睬捶、創(chuàng)建源
File dest=new File("f:/IO/char.txt");
//2黔宛、選擇流
Writer wr=new FileWriter(dest,true);
//3、寫出
String str="鋤禾日當午\r\n碼農(nóng)真辛苦\r\n一本小破書\r\n一讀一上午";
? ? ? wr.write(str);
//追加內(nèi)容
wr.append("我就是追加進去的");
wr.flush();//強制刷出
//4擒贸、關(guān)閉資源
wr.close();
?結(jié)合輸入輸出流臀晃,可以實現(xiàn)文件拷貝
public static void copyFile(String srcPath, String destPath) throws FileNotFoundException,IOException{
// 1觉渴、建立聯(lián)系 源(存在且為文件) 目的地(文件可以不存在)
File src = new File(srcPath);
File dest = new File(destPath);
if(!src.isFile()){//不是文件或者為null時拋出異常
System.out.println("只能拷貝文件");
throw new IOException("只能拷貝文件");
}
// 2、選擇流
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
// 3徽惋、操作
byte[] flush = new byte[1024];
int len = 0;
// 讀取
while (-1 != (len = in.read(flush))) {
// 寫出
out.write(flush, 0, len);
}
out.flush();// 強制刷出
// 關(guān)閉流 先打開的后關(guān)閉
out.close();
in.close();
}
3案淋、緩沖處理流:BufferedOutputStream 和 BufferedWriter
緩沖流提升性能,BufferedWriter存在新增方法newLine()险绘,不能發(fā)生多態(tài)
public static void copyFile(String srcPath, String destPath) throws FileNotFoundException,IOException{
// 1踢京、建立聯(lián)系 源(存在且為文件) 目的地(文件可以不存在)
File src = new File(srcPath);
File dest = new File(destPath);
if(!src.isFile()){//不是文件或者為null時拋出異常
System.out.println("只能拷貝文件");
throw new IOException("只能拷貝文件");
}
// 2、選擇流
InputStream in = new BufferedInputStream(new FileInputStream(src));
OutputStream out =new BufferedOutputStream(new FileOutputStream(dest));
// 3宦棺、操作
byte[] flush = new byte[1024];
int len = 0;
// 讀取
while (-1 != (len = in.read(flush))) {
// 寫出
out.write(flush, 0, len);
}
out.flush();// 強制刷出
// 關(guān)閉流 先打開的后關(guān)閉
out.close();
in.close();
}
}? ?
? ? ? ? ? ? ? ? ? ? ? //1瓣距、創(chuàng)建源 ? 僅限于 字符的純文本
File src=new File("f:/char.txt");
File dest=new File("f:/testIO/char.txt");
//2、選擇流
BufferedReader reader=new BufferedReader(new FileReader(src));
BufferedWriter wr=new BufferedWriter(new? FileWriter(dest,true));pend(msg2);
? ? ? ? ? ? ? ? ? ? //3代咸、新增方法操作
String line=null;
while(null!=(line=reader.readLine())){
wr.write(line);
//wr.append("\r\n");
//換行符號
wr.newLine();
}
wr.flush();//強制刷出
// 4旨涝、關(guān)閉流 先打開的后關(guān)閉
out.close();
in.close();
4、轉(zhuǎn)換處理流:OutputStreamWriter
可以處理文件的字符集侣背,即將文件按指定字符集進行編碼存儲 。
//寫出文件 編碼
BufferedWriter bw=new BufferedWriter(
new OutputStreamWriter(
new BufferedOutputStream(
new FileOutputStream(
new File("f:/testIO/char.txt")
)
),"utf-8"
)
);
String info=null;
while(null!=(info=br.readLine())){
bw.write(info);
bw.newLine();
}
bw.flush();
bw.close();
5慨默、字節(jié)數(shù)組節(jié)點類: ByteArrayOutputStream
/**
* 字節(jié)數(shù)組輸出流:操作與文件輸出流有些不同贩耐,有新增方法,所以不可以使用多態(tài)
* @throws IOException
*/
public static byte[] write() throws IOException{
//目的地 字節(jié)數(shù)組
byte[]dest;
//選擇流 不同點:不需要將目的地放入new ByteArrayOutputStream()
ByteArrayOutputStream bos=new ByteArrayOutputStream();
//操作 寫出, 可以當作將本地的內(nèi)容通過字節(jié)數(shù)組寫入服務器
String msg="字節(jié)數(shù)組輸入流:操作與文件輸入流操作一致";
byte[]info=msg.getBytes();
//將內(nèi)容寫入bos
bos.write(info,0,info.length);
//不同點:獲取數(shù)據(jù) toByteArray():是將字節(jié)數(shù)組輸出流轉(zhuǎn)為字節(jié)數(shù)組
dest=bos.toByteArray();
//釋放資源
bos.close();//由于bos在jvm中厦取,所以關(guān)閉與否不影響
return dest;
}
再來看幾個作業(yè)題潮太,大家不妨思考思考。
1虾攻、Reader和Writer的基本特點是?
2铡买、FileReader和FileWriter的作用是?
3、BufferedReader和BufferedWriter的作用是?
4霎箍、word文檔能使用字符流操作嗎?為什么?
5奇钞、使用BufferedReader和BufferedWriter實現(xiàn)文本文件的拷貝
6、什么情況下可以使用字符流拷貝文件夾漂坏?什么情況下不能景埃?拷貝文件夾應該使用字符流還是字節(jié)流?
7、拷貝文件 使用哪些流顶别?
8谷徙、InputStreamReader和OutputStreamWriter的作用。
9驯绎、ByteArrayInputStream與 ByteArrayOutputStream的數(shù)據(jù)源是什么?
10完慧、為什么ByteArrayOutputStream 不推薦使用匿名?
11剩失、將”堅信沒有學不會的知識屈尼,只有不想學的知識”寫出到字節(jié)數(shù)組中册着。
12、從上述的 字節(jié)數(shù)組中鸿染,讀取字符串指蚜。
13、DataInputStream和DataOutputStream的特點是?
14涨椒、將3.14 寫出到字節(jié)數(shù)組中摊鸡,再進行讀取
15、序列化和反序列化指的是什么?
16蚕冬、想序列化某個類的對象免猾,該類必須實現(xiàn)Serializable接口嗎?
17、說說Serializable接口的特點囤热。
18猎提、transient的作用是?
19旁蔼、使用ObjectInputstream和ObjectOutputStream實現(xiàn)將某個對象存儲到硬盤上锨苏,然后再讀到程序中。
20棺聊、PrintStream打印流經(jīng)常用于什么情況?