1. Create a byte output stream object and do a few things:
(1) Call system function to create file
(2) Create Fos object
(3) Point the Fos object to this file
2. code example:
1 PackageCom.himi.fileoutputstream;2 3 ImportJava.io.File;4 Importjava.io.FileNotFoundException;5 ImportJava.io.FileOutputStream;6 Importjava.io.IOException;7 8 /*9 * Create file Fos.txt, write string: Hello WorldTen * One * Operation flow of the output of the byte stream: A * A: Create byte output stream object - * B: Write Data - * C: Release Resources the */ - - Public classFileOutputStreamDemo1 { - + Public Static voidMain (string[] args)throwsIOException { - //fileoutputstream (file file) + //File File = new file ("Fos.txt"); A //FileOutputStream fos = new FileOutputStream (file); at - //fileoutputstream (String filename) - /** - * Create a byte output stream object to do a few things: - * A: Call system function to create file - * B: Create Fos object in * C: Point the Fos object to this file - */ toFileOutputStream FOS1 =NewFileOutputStream ("Fos.txt"); + - //Write Data theString str =NewString ("Hello,world"); * byte[] bytes =str.getbytes (); $ fos1.write (bytes);Panax Notoginseng //frees up resources to make stream objects garbage so that they can be reclaimed by the garbage collector - fos1.close (); the + } A the}
The output results are as follows:
Java Fundamentals Hardening IO Flow Note 18:fileoutputstream writing data