java file and io--byte array stream data stream string stream

Source: Internet
Author: User



byte array stream



Bytearrayinputstream: contains an internal buffer that contains bytes read from the stream. The internal counter tracks the next byte to be provided by the Read method. Closing Bytearrayinputstream is not valid. The methods in this class can still be called after the stream is closed, without generating any ioexception.



bytearrayoutputstream: An output stream is implemented in this class. Where the data is written to a byte array. The buffer grows automatically as data is written continuously. Data can be obtained using Tobytearray () and ToString (). Closing Bytearrayoutputstream is not valid. The methods in this class can still be called after the stream is closed, without generating any ioexception.



Cases:

public class ByteArrayStream {

    / **
     * @param args
     * /
    public static void main (String [] args) {
        // TODO Auto-generated method stub
        byteArrayStream ();
    }
    public static void byteArrayStream () {
        ByteArrayInputStream bai = new ByteArrayInputStream ("哈哈哈" .getBytes ());
        ByteArrayOutputStream bao = new ByteArrayOutputStream ();
        int i = -1;
        while ((i = bai.read ())! =-1) {
            bao.write (i);
        }
        System.out.println (bao.toString ());
    }

}
data flow

DataInputStream: The data input stream allows applications to read basic Java data types from the underlying input stream in a machine-independent manner. Applications can use the data output stream to write data that is later read by the data input stream. DataInputStream is not necessarily safe for multi-threaded access. Thread safety is optional, and it is the responsibility of users of such methods.

DataOutputStream: The data output stream allows applications to write basic Java data types to the output stream in an appropriate manner. The application can then use the data input stream to read in the data.

public class DataDemo {

    / **
     * @param args
     * /
    public static void main (String [] args) {
        // TODO Auto-generated method stub
        DataOut ();
        DataIn ();
    }
    public static void DataOut () {
        try {
            OutputStream out = new FileOutputStream ("G: \\ bbb.txt");
            DataOutputStream dos = new DataOutputStream (out);
            dos.writeInt (6); // Write 4 bytes
            dos.writeUTF ("quack quack");
            dos.writeLong (1512322);
            dos.close ();
            out.close ();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace ();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace ();
        }
        
    }
    public static void DataIn () {
        try {
            InputStream in = new FileInputStream ("G: \\ bbb.txt");
            DataInputStream dis = new DataInputStream (in);
            int a = dis.readInt ();
            String s = dis.readUTF ();
            long b = dis.readLong ();
            System.out.println (a);
            System.out.println (s);
            System.out.println (b);
            dis.close ();
            in.close ();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace ();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace ();
        }
    }
}
String stream

StringReader: a character stream whose source is a string.

StringWriter: A character stream that can be used to construct a string by recycling the output in the string buffer. Closing StringWriter is invalid. The methods in this class can still be called after closing the stream without generating any IOException.

public static void main (String [] args) {
        // TODO Auto-generated method stub
        stringStream ();
    }
    public static void stringStream () {
        String s = "Simple programming";
        StringReader sr = new StringReader (s);
        char [] c = new char [2];
        int len = -1;
        StringBuffer sb = new StringBuffer ();
        try {
            while ((len = sr.read (c))! =-1) {
                sb.append (c, 0, len);
                
            }
            System.out.println (sb);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace ();
        }
    }
}
 

Java files and io-byte array stream data stream string stream

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.