Java I/O系统之OutputStream

来源:互联网 发布:2017fc2破解版域名设置 编辑:程序博客网 时间:2024/05/23 00:10

1.OutputStream类型

继续自OutputStream的流是用于程序中输入数据,且数据的单位字节(8bit):下图深色为节点流,浅色为处理流。


2.OutputStream的基本方法

OutputStream的基本方法如下:

1)        向输出流写入一个字节数据,该字节数据为参数b的低8位

[java] view plain copy
print?
  1. void write(int b) throws IOException  

2)        将一个字节类型的数组中的数据写入输出流。

[java] view plain copy
print?
  1. void write(byte[] b) throws IOEception  

3)        将一个字节类型的数组中的从指定位置(off)开始len个字节写入到输出流。

[java] view plain copy
print?
  1. void write(byte[] b,int off,int len) throws IOException  

4)        关闭流释放内存资源

[java] view plain copy
print?
  1. void close() throws IOException  

5)        将输出流中缓存的数据全部写出到目的地

[java] view plain copy
print?
  1. void flush() throws IOException  

3.OutputStream的例子

[java] view plain copy
print?
  1. package com.owen.io;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. /** 
  8.  * 写入文件 FileOutputStream 
  9.  * @author OwenWilliam 2016-7-19 
  10.  * @since 
  11.  * @version v1.0.0 
  12.  * 
  13.  */  
  14. public class TestFileOutputStream  
  15. {  
  16.   
  17.     public static void main(String[] args)  
  18.     {  
  19.         int b = 0;  
  20.         FileInputStream in = null;  
  21.         FileOutputStream out = null;  
  22.           
  23.         try  
  24.         {  
  25.             in = new FileInputStream("E:\\workspace\\Java\\IO\\src\\com\\owen\\io\\TestFileInputStream.java");  
  26.             out = new FileOutputStream("E:\\workspace\\Java\\IO\\src\\com\\owen\\io\\TestFileInputStream2.java");  
  27.               
  28.             while ((b = in.read()) != -1)  
  29.             {  
  30.                 out.write(b);  
  31.             }  
  32.               
  33.             in.close();  
  34.             out.close();  
  35.         } catch (FileNotFoundException e)  
  36.         {  
  37.             System.out.println("找不到指定文件");  
  38.             System.exit(-1);  
  39.         }catch (IOException el)  
  40.         {  
  41.             System.out.println("文件复制错误");  
  42.             System.exit(-1);  
  43.         }  
  44.           
  45.         System.out.println("文件已复制");  
  46.   
  47.     }  
  48.   
  49. }  







原创粉丝点击