The file output stream is a byte stream class used to process raw binary data. In order to write data to a file, the data must be converted to bytes and saved to a file.
Implementation: Create file path, create file, write file contents. The code is as follows:
1 ImportJava.io.BufferedReader;2 ImportJava.io.File;3 ImportJava.io.FileOutputStream;4 Importjava.io.IOException;5 ImportJava.io.InputStreamReader;6 7 Public classIOControl8 {9 Ten Public Static voidMain (string[] args)throwsIOException One { ASystem.out.println ("Please enter file address:"); -BufferedReader br =NewBufferedReader (NewInputStreamReader (system.in)); -String fileadress =br.readline (); theFile File =NewFile (fileadress); - if(!file.exists ()) - { - File.mkdir (); + } -System.out.println ("Please enter a filename"); +BufferedReader BR1 =NewBufferedReader (NewInputStreamReader (system.in)); AString filename =br1.readline (); atFile file1=NewFile (fileadress,filename); - if(!file1.exists ()) - { - file1.createnewfile (); - } -System.out.println ("Please enter the contents of the file:"); inBufferedReader Br2 =NewBufferedReader (NewInputStreamReader (system.in)); -String content =br2.readline (); to //Jdk7 New ways to create files +FileOutputStream FOP =NewFileOutputStream (file1); - byte[] Byteincontent =content.getbytes (); the Fop.write (byteincontent); * Fop.flush (); $ fop.close ();Panax NotoginsengSystem.out.println ("File Creation complete"); - the } + A the + -}
It uses the JDK7 resource shutdown method to process the file.
Java small exercise, io manipulation.