Java byte of the output stream [the OutputStream]

First, the output byte stream

  java.io.OutputStream abstract class is a superclass of all output stream of bytes, the byte information is written to the specified destination.

  This class defines the basic functionality common method output stream of bytes.

  Public methods:

public void close (): Close this output stream and releases any system resources associated with the stream. 
public void flush (): Flushes this output stream and forces any buffered output bytes to be written. 
public void write (byte [] b ): The b.length byte to this output stream from the specified byte array. 
public void write (byte [] b , int off, int len): write len bytes from the specified byte array, an output start offset off to this output stream. 
public abstract void write (int b) : the specified output stream of bytes.

  Note : close method, when the flow of the operation is complete, you must call this method to release system resources.

   Extended :

   Flow node : node data stream can be read from a particular data source (node) (eg: files, memory)

   Process Flow : process flow is "attached to the existing flow (process flow stream or node) above, to provide a more robust read and write functions for the processing of data by the program.

 

  The java.io.OutputStream to as an abstract class, there are many subclasses below.

  

   Dark node in FIG stream, a light stream is processed.

Two, FileOutputStream class

  java.io.FileOutputStream class file output stream for writing data to a file, is a simple OutputStream subclass.

  1, construction method

FileOutputStream (String name) Creates a write data to a file with the specified name of the output file stream. 
 FileOutputStream (File file) to create a file for writing data file represented by the specified File object in the output stream.

    Parameters : destination data is written.

      String name: the destination is the path to a file.

      File file: the destination is a file.

    The role of the constructor :

      ① create a FileOutputStream object

      ② based file transfer constructor / file path, create an empty file

      ③ will FileOutputStream object pointing to the created file 

   Principle write data:

    java program -> JVM (java virtual machine) -> OS (Operating System) -> OS calls the method of writing data -> write data to a file

   Use step output stream of bytes [ Important ]:

     ① create a FileOutputStream object, the construction method of writing data transmission destination

     ② call the method write FileOutputStream object, the data is written to a file

     ③ release resources (using flow will occupy some memory, memory emptied after use should provide efficiency of the program)

  2, write data bytes

    (1) Write Byte : using write (int b) methods, each can write a byte of data, use the code demonstrates:

. 1   public  static  void main (String [] args) throws IOException {
 2          // 1. Create a FileOutputStream object constructor write data transfer destination 
. 3          FileOutputStream fos = new new FileOutputStream ( "E: \\ a.txt" );
 4          // invoke method write FileOutputStream object, writes data to a file
 . 5          // public abstract void write (int B): the specified output stream of bytes. 
. 6          fos.write (97 );
 . 7          // 3. released resource (stream will occupy some memory use, the used empty memory should provide efficient program)
 . 8          // fos.close (); 
. 9      }

    principle:

 

    (2) Write a byte array : the use of Write (byte [] B) , each time the data can be written in the array, the code demo:

 1 public static void main(String[] args) throws IOException {
 2         //创建FileOutputStream对象,构造方法中绑定要写入数据的目的地
 3         FileOutputStream fos = new FileOutputStream(new File("E:\\b.txt"));
 4         //调用FileOutputStream对象中的方法write,把数据写入到文件
 5 
 6         /*
 7             public void write(byte[] b):将 b.length字节从指定的字节数组写入此输出流。
 8             一次写多个字节:
 9                 如果写的第一个字节是正数(0-127),那么显示的时候会查询ASCII表
10                 如果写的第一个字节是负数,那第一个字节会和第二个字节,两个字节组成一个中文显示,查询系统默认码表(GBK)
11          */
12         byte[] bytes = {65,66,67,68,69};//ABCDE
13         //byte[] bytes = {-65,-66,-67,68,69};//烤紻E
14         fos.write(bytes);
15 
16         /*
17             写入字符的方法:可以使用String类中的方法把字符串,转换为字节数组
18                 byte[] getBytes()  把字符串转换为字节数组
19          */
20         byte[] bytes2 = "你好".getBytes();
21         System.out.println(Arrays.toString(bytes2));//[-28, -67, -96, -27, -91, -67]
22         fos.write(bytes2);
23 
24         //释放资源
25         fos.close();
26     }

 

    (3)写出指定长度字节数组write(byte[] b, int off, int len) ,每次写出从off索引开始,len个字节,代码使用演示:

 1 public static void main(String[] args) throws IOException {
 2         //创建FileOutputStream对象,构造方法中绑定要写入数据的目的地
 3         FileOutputStream fos = new FileOutputStream(new File("09_IOAndProperties\\b.txt"));
 4 
 5         /*
 6             public void write(byte[] b, int off, int len) :把字节数组的一部分写入到文件中
 7                 int off:数组的开始索引
 8                 int len:写几个字节
 9          */
10         byte[] bytes = {65,66,67,68,69};//ABCDE
11         fos.write(bytes,1,2);  //BC
12 
13         //释放资源
14         fos.close();
15     }

 

  3、数据追加续写

    追加写/续写:使用两个参数的构造方法

FileOutputStream(String name, boolean append)创建一个向具有指定 name 的文件中写入数据的输出文件流。
FileOutputStream(File file, boolean append) 创建一个向指定 File 对象表示的文件中写入数据的文件输出流。

    参数

      String name,File file:表示写入数据的目的地。

      boolean append :是否开启追加

          true:创建对象不会覆盖源文件,继续在文件的末尾追加写数据

          false:创建一个新文件,覆盖源文件

    Demo:

1 public static void main(String[] args) throws IOException {
2         FileOutputStream fos = new FileOutputStream("E:\\c.txt",true);
3         for (int i = 1; i <=10 ; i++) {
4             fos.write("你好".getBytes());
5         }
6 
7         fos.close();
8     }

 

  4、写出换行

    可以发现写出的内容到文件内都是在一行内显示,没有换行。

    回车符 \r 和换行符 \n

      •   回车符:回到下一行开头(return)
      •   换行符:下一行(newline)

    写换行:需要写换行符号。

    系统中的换行:

      •   Windows系统里,每行结尾是 回车+换行 ,即 \r\n
      •   Linux / Unix系统里,每行结尾只有 换行 ,即 \n
      •   Mac系统里,每行结尾是 回车 ,即 \r 。从 Mac OS X开始与Linux统一。

 

    Demo :

1 public static void main(String[] args) throws IOException {
2         FileOutputStream fos = new FileOutputStream("E:\\c.txt",true);
3         for (int i = 1; i <=10 ; i++) {
4             fos.write("你好".getBytes());
5             fos.write("\r\n".getBytes());
6         }
7 
8         fos.close();
9     }

 

 

Guess you like

Origin www.cnblogs.com/niujifei/p/11487223.html