javase02-unit07: Basic IO operation, text data io operation

Source: Internet
Author: User

Basic IO operations, text data IO operationsJava standard IO (input/output) operations
 Packageday07;ImportJava.io.FileOutputStream;Importjava.io.IOException;/*** java standard IO (input/output) operation * Input stream Inputstrean: used to read data from the data source into the program * output stream OutputStream: used to send data to the target * stream divided into: node flow, processing Flow * Node Flow: Also known as low-level Flow, Characteristics: Data source Clear, true negative * responsibility "handling" data stream reading and writing is based on the node stream * Processing flow: Also called Advanced streaming, features: Can not exist independently (meaningless), * to handle other streams, to simplify the read and write operation * file stream * File flow is low Stream, because the data source is clear, is read and write file * * File output stream: Java.io.FileOutputStream * for writing data to a file * *@authorAdminitartor **/ Public classFileoutputstream_write { Public Static voidMain (string[] args)throwsIOException {/** There are two common patterns for file streams: * Overwrite write operations when the constructor method passes only one parameter: * FileOutputStream (String path) * Fileoutputstre         AM (File file) * If the file already exists, all data in the file will be purged first. * * Append write operation: * FileOutputStream (String path,boolean Append) * FileOutputStream (File File,boolean AP         Pend) * If the second argument is true, the content written out through the current stream will be appended to the end of the file. */FileOutputStream Fos=NewFileOutputStream ("Fos.txt",true            ); String Str= "I do not turn a blind eye to the match you play!"; Fos.write (Str.getbytes ("UTF-8")); System.out.println ("Write it Out!");    Fos.close (); }}
Fileoutputstream_write.javaFile input stream, a stream that reads bytes from a file, is a low-level stream
 Packageday07;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;/*** java.io.FileInputStream * File input stream * used to read bytes from a file stream, is a low-level stream * *@authorAdminitartor **/ Public classFileinputstream_read { Public Static voidMain (string[] args)throwsIOException {fileinputstream fis=NewFileInputStream ("Fos.txt"); byte[] data =New byte[200]; intLen =fis.read (data); String Str=NewString (Data,0,len, "UTF-8");                    System.out.println (str);            Fis.close (); }}
Fileinputstream_read.javaCopying files using a file stream
 Packageday07;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;/*** Copy files using a file stream *@authorAdminitartor **/ Public classcopydemo1{ Public Static voidMain (string[] args)throwsIOException {fileinputstream fis=NewFileInputStream ("Music.mp3"); FileOutputStream Fos=NewFileOutputStream ("Music_cp2.mp3"); byte[] buf =New byte[1024*10]; intLen =-1;  while(len = Fis.read (buf))!=-1) {fos.write (buf,0, Len); } System.out.println ("Copy complete!");        Fis.close ();    Fos.close (); }}
Copydemo1.javaBuffered stream buffer streams are a pair of premium streams that can be used to improve read and write efficiency Java.io.BufferedOutputStream buffered byte output stream for improved writing efficiency
 Packageday07;ImportJava.io.BufferedInputStream;ImportJava.io.BufferedOutputStream;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;/*** Buffered Stream Buffer stream is a pair of advanced streams that use them to improve read and write efficiency Java.io.BufferedOutputStream buffered byte output stream for increased write efficiency * * Java.io.BufferedInputStream buffered byte input stream For improved read efficiency * *@authorAdminitartor **/ Public classCopyDemo2 { Public Static voidMain (string[] args)throwsIOException {fileinputstream fis=NewFileInputStream ("Mv.mp4"); Bufferedinputstream bis=NewBufferedinputstream (FIS); FileOutputStream Fos=NewFileOutputStream ("Mv_cp.mp4"); Bufferedoutputstream Bos=NewBufferedoutputstream (FOS); intD =-1; LongStart =System.currenttimemillis ();  while((d = bis.read ())! =-1) {bos.write (d); }        LongEnd =System.currenttimemillis (); System.out.println ("Copy finished!" Time: "+ (End-start) +" MS ");        Bis.close ();    Bos.close (); }}
Copydemo2.javaBuffer Stream writes out data buffers problem
 Packageday07;ImportJava.io.BufferedOutputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;/*** Buffer Stream writes out data buffers problem *@authorAdminitartor **/ Public classBos_write { Public Static voidMain (string[] args)throwsIOException {fileoutputstream fos=NewFileOutputStream ("Bos.txt"); Bufferedoutputstream Bos=NewBufferedoutputstream (FOS); String Str= "I have a pen,i has a apple";        Bos.write (Str.getbytes ()); /** void Flush () * This method can force the data in the buffer in the current buffer stream to be written out. */Bos.flush (); System.out.println ("Write it Out!");    Bos.close (); }}
Bos_write.java

javase02-unit07: Basic IO operation, text data io operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.