JAVA Inputstream/outputstream byte input stream, output stream read-write file __java

Source: Internet
Author: User
Tags throw exception

A good memory is better than a pen.
Note: Inputstream/outputstream is an abstract class, cannot be instantiated, can only instantiate its subclasses, and is a byte stream, not a string

InputStream is = new FileInputStream ("A.txt"); Equivalent to InputStream is = new FileInputStream (New File ("A.txt"));

OutputStream similar to the same,

Package com.

Ckinghan.outputstream;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;

Import Java.io.OutputStream; public class Outputstreamdemo {public static void main (string[] args) {/** * writes a file using a byte stream *

        /Outputstreadread ();

        /** * Byte stream data reading mode one: Every time read a bytes * * InputStreamReader ();


        /** * Byte stream data reading mode two: each read a certain length of bytes, recommended to use/InputStreamReader1 ();
    /** * Byte stream data reading mode three: Save the data from the specified array index every time a certain length is read * * * INPUTSTREAMREADER2 (); /** * @ Description: Data reading method of byte stream three: Save data from specified array index every time a certain length is read * Create time: */public static void InputStreamReader
        2 () {///Create byte object InputStream inputstream = null;

            try {//instantiated object InputStream = new FileInputStream ("Javaiooutputstreamreader.java"); /** * per readA length of byte saves data from a specified array index and can read Chinese (the data in the file is: Outputstreamp test write data)//define the size of each read byte and the data that holds the byte byt
            E[] bs = new byte[1024];
            Defines the length of each read int len =-1; /** * Inputstream.read (bs,1,10); Parameter Description: * BS: Array, maximum can save 1024 bytes each time * 1: The specified index position, here is the index from the BS array 1 start to save data * 10: From InputStream
            Number of bytes read, starting from 0, note that if the read data is in Chinese and the number of bytes read is odd, it may be unrecognized or incorrect data/len = Inputstream.read (bs,1,10); 

        Output character, note: At the time of output, read System.out.println (new String (Bs,1,len)) from the specified index position;
        catch (FileNotFoundException e) {e.printstacktrace ();
        }catch (IOException e) {e.printstacktrace (); }finally {//close stream if (InputStream!= null) {try {inputstream.cl
                OSE ();
                catch (IOException e) {e.printstacktrace ();



}
            }
        }
    }    /** * Description: Byte stream data read mode Two: Every time to read a certain length of bytes, recommended to use the * @ Create Time: * * public static void InputStreamReader1 () {
        Creates a Byte object InputStream inputstream = null;

            try {//instantiated object InputStream = new FileInputStream ("Javaiooutputstreamreader.java");
             /** * Read a certain length of byte data, recommended to use, can read Chinese (the data in the file is: Outputstreamp Test write data) * Read the result is: Outputstreamp Test write data
            //define the size of bytes per read and save byte data byte[] bs = new byte[1024];
            Defines the length of each read int len =-1;
                Read the data circularly, if read the data is-1, the description has read the end while (len = Inputstream.read (BS))!=-1) {//Output characters 
            System.out.println (New String (Bs,0,len));
        } catch (FileNotFoundException e) {e.printstacktrace ();
        }catch (IOException e) {e.printstacktrace ();
                 }finally {//off stream if (InputStream!= null) {try {   Inputstream.close ();
                catch (IOException e) {e.printstacktrace (); /** * @ Description: Data read by byte stream one bytes per read * @ Create time:/public static void in
        Putstreamreader () {///create object InputStream InputStream = null;
            try {//instantiated object InputStream = new FileInputStream ("Javaiooutputstreamreader.java");
             /** * read one byte at a time, it is OK for English (the data in the file is: Outputstreamp test write data), * but cannot read Chinese, because the encoding I use is UTF-8, one Chinese occupies two byte length
             * Read the results are as follows: Outputstreamp????????????
            *///create read characters, save corresponding acsii code int b =-1;
                Loops read the character, if the character is-1, the read to the end of the file while ((b = inputstream.read ())!=-1) {//Convert the read acsii code to characters and output
            System.out.print ((char) b);
        } System.out.println ();
        catch (FileNotFoundException e) {e.printstacktrace (); }catcH (IOException e) {e.printstacktrace (); }finally {//close stream if (InputStream!= null) {try {inputstream.cl
                OSE ();
                catch (IOException e) {e.printstacktrace (); /** * @ Description: Use byte stream to write data to file * Creation time: */public static void Outputstre
        Adread () {///Create byte throttle object OutputStream stream = null;
            try {//Instantiate object stream = new FileOutputStream ("Javaiooutputstreamreader.java");
            The string data to write String = "Outputstreamp test write Data";
            Converts string data to byte array byte[] bytes = String.getbytes ();
            Writes a byte array to the file Stream.Write (bytes);
            Empty the buffer and save the written data Stream.flush ();

            Successful write prompt System.out.println ("Write file succeeded");
Throw exception} catch (IOException e) {e.printstacktrace ();        }finally {//If stream is instantiated if (stream!= null) {try {//
                Turn off byte stream stream.close ();
                catch (IOException e) {e.printstacktrace ();
 }
            }
        }
    }

}
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.