IO Stream Basic operations

Source: Internet
Author: User

The first method of division:1. Input stream 2. Output stream The second method of division:1. Byte stream 2. The third type of character stream:1. Node stream 2. Process Flow////////////////////////////////////////////////////////////////////////////byte stream:What is the subclass of InputStream? Fileinputstream;inputstream:intReadbyte[]b,intOffintlen) read from the first, read a few, return the number of reads OutputStream subclass is? Fileoutputstreamoutputstream:voidWritebyte[]b,intOffintlen) Public Static voidMain (String args[]) {FileInputStreaminch=NULL; FileOutputStream out=NULL; Try {            inch=NewFileInputStream ("E:/from.txt");//input stream (read data from a file, i.e. data input into the program)             out=NewFileOutputStream ("E:/to.txt");//output Stream (writes file data, i.e. data output into a file)            byte[] buffer =New  byte[ -];//an array of bytes to hold the Read data            intnum =inch. read (buffer,0, buffer.length);//read from the No. 0 bit, read the buffer.length of the From.txt file, put it in buffer             out. write (buffer,0, num);//The Read method returns the number of reads//out.write (buffer, 0, 2);//starting with 0 bits, write the 2 bytes of the buffer array into the To.txt file .             for(inti =0; i<buffer.length;i++) {System. out. Print (Buffer[i]); }string s=NewString (buffer);//Convert to original characterS= S.trim ();//Trim Method: Remove the leading and trailing blanks and empty characters, the middle space is not removedSystem. out. Print (s); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{            Try {                inch. Close ();  out. Close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }}} read large files: Public Static voidMain (String args[]) {FileInputStreaminch=NULL; FileOutputStream out=NULL; intA =0; Try {            inch=NewFileInputStream ("E:/from.txt");//input stream (read data from a file, i.e. data input into the program)             out=NewFileOutputStream ("E:/to.txt");//output Stream (writes file data, i.e. data output into a file)            byte[] buffer =New  byte[1024x768];//an array of bytes to hold the Read data             while(true) {a++; System. out. Print (a); intnum =inch. read (buffer,0, buffer.length);//read from the No. 0 bit, read the buffer.length of the From.txt file, put it in buffer                 out. write (buffer,0, num);//The Read method returns the number of reads                if(num = =-1){//returns-1, indicating that the read is complete                     Break; }            }                } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }        finally{            Try {                inch. Close ();  out. Close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }////////////////////////////////////////Character stream: characters-based when reading and writing filesbyte input stream: What is the subclass of Reader? FileReaderintReadChar[] C,intOffintlen) Bytes output stream: What is the subclass of Writer? FileWritervoidWriteChar[] C,intOffintlen) Public Static voidMain (String args[]) {FileReader reader=NULL; FileWriter writer=NULL; Try{Reader=NewFileReader ("E:/from.txt"); Writer=NewFileWriter ("E:/to.txt"); Char[] buffer =New Char[ -]; intnum = reader.read (buffer,0, buffer.length); Writer.write (Buffer,0, num);  for(inti =0; i<num;i++) {System. out. Print (Buffer[i]); }        }        Catch(Exception e) {System. out. Print (e); }        finally{            Try{reader.close ();            Writer.close (); }            Catch(Exception e) {System. out. Print ("Success!!!"); }        }}/////////////////////////////////////////////////read one row of data at once Public Static voidMain (String args[]) {FileReader FileReader=NULL; BufferedReader BufferedReader=NULL; Try{FileReader=NewFileReader ("E:/from.txt"); BufferedReader=NewBufferedReader (FileReader); String Line=NULL;  while(true) { line=Bufferedreader.readline (); if(line = =NULL){                 Break; } System. out. println (line); }        }        Catch(Exception e) {System. out. println (e); }        finally{            Try{bufferedreader.close ();            Filereader.close (); }            Catch(Exception e) {System. out. println (e); }                    }

IO Stream Basic operations

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.