Java Fundamentals-Input/Output (ii)

Source: Internet
Author: User
Tags finally block

I. Byte stream and character stream

1.1 InputStream and Reader

InputStream is an abstract class that itself cannot create an instance to perform the input, which contains the following methods:

1. Int Read (): reads a single byte from the input stream, returning the byte data that was read. 2.intReadbyte[] b): reads up to b.length bytes of data from the input stream and stores it in byte array B, returning the actual number of bytes read. 3.intReadbyte[] B,intOffintlen): reads up to len bytes of data from the input stream and stores it in array B, in array B, not starting from the beginning of the array, but starting from the off position, returning the actual number of bytes read. 4.intAvailable ()throwsIOException: Returns the number of valid bytes in the stream ...LongSkipLongNthrowsioexception: Ignores numbytes bytes, returns the number of bytes actually ignored6.Abstract voidClose ()throwsIOException: Turn off input

As mentioned earlier, InputStream and reader are abstract classes, they cannot create instances themselves, but they each have an input stream for reading the file: Fileiputstream and Fileread, which are node streams-directly associated with the specified file.

The following example uses Fileiputstream to read itself as an effect.

ImportJava.io.*; Public classfileinputstreamtest{ Public Static voidMain (string[] args)throwsIOException {//Create a byte input streamFileInputStream FIS =NewFileInputStream ("D:/j2se1/testworkspace/test1234/src/fileinputstreamtest.java")///Here I use an absolute address//Create a "bamboo" with a length of 1024        byte[] Bbuf =New byte[1024]; //the number of bytes used to hold the actual read        intHasread = 0; //use loops to repeat the "fetch water" process         while((Hasread = Fis.read (bbuf)) > 0 )        {            //Remove the droplets (bytes) from the "Bamboo tube" and convert the byte array into a string input! System.out.print (NewString (bbuf, 0, Hasread)); }        //close the file input stream and put it in the finally block more secureFis.close (); }}

The results are as follows:

ImportJava.io.*; Public classfileinputstreamtest{ Public Static voidMain (string[] args)throwsIOException {//Create a byte input streamFileInputStream FIS =NewFileInputStream ("D:/j2se1/testworkspace/test1234/src/fileinputstreamtest.java"); //Create a "bamboo" with a length of 1024        byte[] Bbuf =New byte[1024]; //the number of bytes used to hold the actual read        intHasread = 0; //use loops to repeat the "fetch water" process         while((Hasread = Fis.read (bbuf)) > 0 )        {            //Remove the droplets (bytes) from the "Bamboo tube" and convert the byte array into a string input! System.out.print (NewString (bbuf, 0, Hasread)); }        //close the file input stream and put it in the finally block more secureFis.close (); }}

1.2 OutputStream and writer

Outputstrean and writer are very similar. Two streams provide the following methods:

1.void2. void  3.void write (byte[] b): writes B.length bytes from the specified byte array to this input        4.  void Write (byteintint  len: Specifies the byte array from the offset of off         5.  Abstractvoid write (int b): Writes the specified bytes to this output stream.

The following program uses FileInputStream to perform the input and uses FileOutputStream to perform the output, which is used to implement the function of copying the Fileoutpputstreamtest.java file.

ImportJava.io.*; Public classfileoutputstreamtest{ Public Static voidMain (string[] args) {Try(            //Create a byte input streamFileInputStream FIS =NewFileInputStream ("Fileoutputstreamtest.java"); //creating a byte output streamFileOutputStream fos =NewFileOutputStream ("NewFile.txt"))        {            byte[] Bbuf =New byte[32]; intHasread = 0; //loops out data from the input stream             while((Hasread = Fis.read (bbuf)) > 0 )            {                //every read, that is, write the file output stream, read how much, just write how much. Fos.write (bbuf, 0, Hasread); }        }        Catch(IOException IoE) {ioe.printstacktrace (); }    }}

Java Fundamentals-Input/Output (ii)

Related Article

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.