大家好,樂字節小樂又來了。上一篇給大家帶來的是:Java中的IO流之輸入流|樂字節,本文將繼續講述IO流之輸出流。
一、輸出流
1、抽象類:OutputStream 和 Writer
OutputStream和Writer也非常相似。
在OutputStream 裏包含如下方法:
在 Writer 中, 因爲字符流直接以字符作爲操作單位,所以 Writer 可以用字符串來代替字符數組,即以String對象來作爲參數。 包含如下方法:
2、文件節點類: FileOutputStream 和 FileWriter
FileOutputStream 和 FileWriter,它們都是節點流,直接和指定文件關聯。
public class WriteFile {
public static void main(String[] args) {
//1、建立聯繫 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";
//字符串轉成字節數組
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("關閉輸出流失敗");
}
}
}
}
//1、創建源
File dest=new File("f:/IO/char.txt");
//2、選擇流
Writer wr=new FileWriter(dest,true);
//3、寫出
String str="鋤禾日當午\r\n碼農真辛苦\r\n一本小破書\r\n一讀一上午";
wr.write(str);
//追加內容
wr.append("我就是追加進去的");
wr.flush();//強制刷出
//4、關閉資源
wr.close();
結合輸入輸出流,可以實現文件拷貝
public static void copyFile(String srcPath, String destPath) throws FileNotFoundException,IOException{
// 1、建立聯繫 源(存在且爲文件) 目的地(文件可以不存在)
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();// 強制刷出
// 關閉流 先打開的後關閉
out.close();
in.close();
}
3、緩衝處理流:BufferedOutputStream 和 BufferedWriter
緩衝流提升性能,BufferedWriter存在新增方法newLine(),不能發生多態
public static void copyFile(String srcPath, String destPath) throws FileNotFoundException,IOException{
// 1、建立聯繫 源(存在且爲文件) 目的地(文件可以不存在)
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();// 強制刷出
// 關閉流 先打開的後關閉
out.close();
in.close();
}
}
//1、創建源 僅限於 字符的純文本
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、關閉流 先打開的後關閉
out.close();
in.close();
4、轉換處理流: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、字節數組節點類: ByteArrayOutputStream
/**
* 字節數組輸出流:操作與文件輸出流有些不同,有新增方法,所以不可以使用多態
* @throws IOException
*/
public static byte[] write() throws IOException{
//目的地 字節數組
byte[]dest;
//選擇流 不同點:不需要將目的地放入new ByteArrayOutputStream()
ByteArrayOutputStream bos=new ByteArrayOutputStream();
//操作 寫出, 可以當作將本地的內容通過字節數組寫入服務器
String msg="字節數組輸入流:操作與文件輸入流操作一致";
byte[]info=msg.getBytes();
//將內容寫入bos
bos.write(info,0,info.length);
//不同點:獲取數據 toByteArray():是將字節數組輸出流轉爲字節數組
dest=bos.toByteArray();
//釋放資源
bos.close();//由於bos在jvm中,所以關閉與否不影響
return dest;
}
再來看幾個作業題,大家不妨思考思考。
1、Reader和Writer的基本特點是?
2、FileReader和FileWriter的作用是?
3、BufferedReader和BufferedWriter的作用是?
4、word文檔能使用字符流操作嗎?爲什麼?
5、使用BufferedReader和BufferedWriter實現文本文件的拷貝
6、什麼情況下可以使用字符流拷貝文件夾?什麼情況下不能?拷貝文件夾應該使用字符流還是字節流?
7、拷貝文件 使用哪些流?
8、InputStreamReader和OutputStreamWriter的作用。
9、ByteArrayInputStream與 ByteArrayOutputStream的數據源是什麼?
10、爲什麼ByteArrayOutputStream 不推薦使用匿名?
11、將”堅信沒有學不會的知識,只有不想學的知識”寫出到字節數組中。
12、從上述的 字節數組中,讀取字符串。
13、DataInputStream和DataOutputStream的特點是?
14、將3.14 寫出到字節數組中,再進行讀取
15、序列化和反序列化指的是什麼?
16、想序列化某個類的對象,該類必須實現Serializable接口嗎?
17、說說Serializable接口的特點。
18、transient的作用是?
19、使用ObjectInputstream和ObjectOutputStream實現將某個對象存儲到硬盤上,然後再讀到程序中。
20、PrintStream打印流經常用於什麼情況?