[Java] BigData basis _FileOutputStream written to the file

Knowledge Point

1. FileOutputStream data is first converted into binary data, and then written to a text file

2.BufferedWriter can be more convenient to write data to a text file.

3 wherein the additional parameter indicates true, the parameter indicates no coverage.

Code

We can FileOutputStream base class character converted to binary, and then written to a file

Package cn.test.logan.day09; 

Import java.io.FileOutputStream; 

public  class FileOutputStreamDemo {
     public  static  void main (String [] args) throws Exception {
         / ** 
         * written using FileOutputStream file 
         * / 
        // cover write mode data 
        a FileOutputStream fos = new new a FileOutputStream ( "D: /demo.txt" ); 
        String S = "Hello 123!" ;
         byte [] bytes = s.getBytes ( "UTF-. 8" ); 
        fos.write (bytes); 
        // Close stream 
        fos.close (); 
        
        //Additional way to write data (add true parameter in FileOutputStream in) 
        FileOutputStream f OS2 = new new FileOutputStream ( "D: /demo.txt", true ); 
        String s2 = ", I'm here." ;
         Byte [] = s2.getBytes bytes2 ( "UTF-. 8" ); 
        fos2.write (bytes2); 
        // Close stream 
        fos2.close (); 
    } 
    
}

But Java provides us with a more advanced BufferedWriter, can more easily achieve data written to the file by the utility class

 

Package cn.test.logan.day09; 

Import java.io.BufferedWriter;
 Import java.io.FileOutputStream;
 Import java.io.OutputStreamWriter; 

public  class BufferedWriterDemo {
     public  static  void main (String [] args) throws Exception {
         / ** 
         * indicates additional parameter to true, indicates the parameter is not covered 
         * OutputStreamWriter may be passed in the coded character set 
         * / 
        BufferedWriter, BF = new new BufferedWriter, ( new new OutputStreamWriter ( new new a FileOutputStream ( "D: /out.dat", to true ), "UTF -8 " ));
        bf.write ("Hello" );
         // here do not add the stream is closed, open not see the contents of the file 
        bf.close (); 
    } 
}

 

Guess you like

Origin www.cnblogs.com/OliverQin/p/12111068.html