java OutputStream的使用


1、

 1 package cn.kongxh.io3;
 2 
 3 import java.io.File ;
 4 import java.io.OutputStream ;
 5 import java.io.FileOutputStream ;
 6 public class OutputStreamDemo01{
 7     public static void main(String args[]) throws Exception{    // 異常拋出,不處理
 8         // 第1步、使用File類找到一個文件
 9         File f= new File("d:" + File.separator + "test.txt") ;    // 聲明File對象
10         // 第2步、通過子類實例化父類對象
11         OutputStream out = null ;    // 准備好一個輸出的對象
12         out = new FileOutputStream(f)  ;    // 通過對象多態性,進行實例化
13         // 第3步、進行寫操作
14         String str = "Hello World!!!" ;        // 准備一個字符串
15         byte b[] = str.getBytes() ;            // 只能輸出byte數組,所以將字符串變為byte數組
16         out.write(b) ;                        // 將內容輸出,保存文件
17         // 第4步、關閉輸出流
18         out.close() ;                        // 關閉輸出流
19         //文件不存在會自動創建
20     }
21 };

2、

 1 package cn.kongxh.io3;
 2 
 3 import java.io.File ;
 4 import java.io.OutputStream ;
 5 import java.io.FileOutputStream ;
 6 public class OutputStreamDemo02{
 7     public static void main(String args[]) throws Exception{    // 異常拋出,不處理
 8         // 第1步、使用File類找到一個文件
 9         File f= new File("d:" + File.separator + "test.txt") ;    // 聲明File對象
10         // 第2步、通過子類實例化父類對象
11         OutputStream out = null ;    // 准備好一個輸出的對象
12         out = new FileOutputStream(f)  ;    // 通過對象多態性,進行實例化
13         // 第3步、進行寫操作
14         String str = "Hello World!!!" ;        // 准備一個字符串
15         byte b[] = str.getBytes() ;            // 只能輸出byte數組,所以將字符串變為byte數組
16         for(int i=0;i<b.length;i++){        // 采用循環方式寫入
17             out.write(b[i]) ;    // 每次只寫入一個內容
18         }
19         // 第4步、關閉輸出流
20         out.close() ;                        // 關閉輸出流
21     }
22 };

3、

 1 package cn.kongxh.io3;
 2 
 3 import java.io.File ;
 4 import java.io.OutputStream ;
 5 import java.io.FileOutputStream ;
 6 public class OutputStreamDemo03{
 7     public static void main(String args[]) throws Exception{    // 異常拋出,不處理
 8         // 第1步、使用File類找到一個文件
 9         File f= new File("d:" + File.separator + "test.txt") ;    // 聲明File對象
10         // 第2步、通過子類實例化父類對象
11         OutputStream out = null ;    // 准備好一個輸出的對象
12         out = new FileOutputStream(f,true)  ;    // 此處表示在文件末尾追加內容
13         // 第3步、進行寫操作
14         String str = "Hello World!!!" ;        // 准備一個字符串
15         byte b[] = str.getBytes() ;            // 只能輸出byte數組,所以將字符串變為byte數組
16         for(int i=0;i<b.length;i++){        // 采用循環方式寫入
17             out.write(b[i]) ;    // 每次只寫入一個內容
18         }
19         // 第4步、關閉輸出流
20         out.close() ;                        // 關閉輸出流
21         
22         /*
23          * 往打開的文件中追加
24          * 
25          * */
26     }
27 };

4、

 1 package cn.kongxh.io3;
 2 
 3 import java.io.File ;
 4 import java.io.OutputStream ;
 5 import java.io.FileOutputStream ;
 6 public class OutputStreamDemo04{
 7     public static void main(String args[]) throws Exception{    // 異常拋出,不處理
 8         // 第1步、使用File類找到一個文件
 9         File f= new File("d:" + File.separator + "test.txt") ;    // 聲明File對象
10         // 第2步、通過子類實例化父類對象
11         OutputStream out = null ;    // 准備好一個輸出的對象
12         out = new FileOutputStream(f,true)  ;    // 此處表示在文件末尾追加內容
13         // 第3步、進行寫操作
14         String str = "\r\nHello World!!!" ;        // 准備一個字符串
15         byte b[] = str.getBytes() ;            // 只能輸出byte數組,所以將字符串變為byte數組
16         for(int i=0;i<b.length;i++){        // 采用循環方式寫入
17             out.write(b[i]) ;    // 每次只寫入一個內容
18         }
19         // 第4步、關閉輸出流
20         out.close() ;                        // 關閉輸出流
21     }
22 };

5、

 1 package cn.kongxh.io3;
 2 
 3 import java.io.File ;
 4 import java.io.OutputStream ;
 5 import java.io.FileOutputStream ;
 6 public class OutputStreamDemo05{
 7     public static void main(String args[]) throws Exception{    // 異常拋出,不處理
 8         // 第1步、使用File類找到一個文件
 9         File f= new File("d:" + File.separator + "test.txt") ;    // 聲明File對象
10         // 第2步、通過子類實例化父類對象
11         OutputStream out = null ;    // 准備好一個輸出的對象
12         out = new FileOutputStream(f)  ;    // 實例化
13         // 第3步、進行寫操作
14         String str = "Hello World!!!" ;        // 准備一個字符串
15         byte b[] = str.getBytes() ;            // 只能輸出byte數組,所以將字符串變為byte數組
16         out.write(b) ;        // 寫入數據
17         // 第4步、關閉輸出流
18         // out.close() ;                        // 關閉輸出流
19         
20         /*
21          * 不關閉 也是會寫入到文件中 證明並沒有使用緩存區
22          * */
23     }
24 };

總結:OutputStream一樣分內存、本地文件、網絡、其他線程管道幾個方向


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM