Io_ Other Stream _ Basic data Type +string processing flow Java158__java

Source: Internet
Author: User
Tags prepare

Source: http://www.bjsxt.com/
One, S02e158_01io_ other stream _ Basic data type +string processing stream
1, base data type +string retention data + type (for machine parsing reading)
Input stream: DataInputStream readxxx ()
Output stream: DataOutputStream writexxx ()

Package com.test.io.others;
Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.ByteArrayInputStream;
Import Java.io.ByteArrayOutputStream;
Import Java.io.DataInputStream;
Import Java.io.DataOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException; /** * Data type (basic +string) Processing stream * 1, input stream DataInputStream readxxx () * 2, output stream DataOutputStream writexxx () * Use new method cannot use polymorphism * * Java.io.EOFException: The end of the file has been reached, not read the relevant content * * public class DataStream {public static void main (string[] args) throws I             oexception {Write ("g:/java/test/data.txt");/Read what you don't understand: @?
        d? data type Read ("G:/java/test/data.txt");
        Read ("G:/java/test/path.java");//illegal content, Java.io.EOFException: Reached the end of the file, not read the relevant content byte[] data = write ();
    Read (data); /** * data + type output to file * @throws IOException */public static void write (String despath) throws Ioexce
    ption{    Double point = 2.5;
        Long num = 100L;
        String str = "data type";
        Create source file des = new file (Despath); Select Stream DataOutputStream DataOutputStream dos = new DataOutputStream (New Bufferedoutputstream
        (New FileOutputStream (DES)));
        The order in which the operation is written must be consistent with the order of reading to prepare dos.writedouble (point);
        Dos.writelong (num);
        Dos.writeutf (str);//String Dos.flush ();
    Dos.close (); /** * Read data from file (basic +string) + type * @throws ioexception/public static void Read (String srcpath) thr
        OWS ioexception{//create source file src = new file (Srcpath); Select stream DataInputStream dis = new DataInputStream (new Bufferedinputstream (new FileInputStream) (s
        RC));
        The order of operation Read is consistent with writing and must exist in order to read//not consistent, data exists double point = dis.readdouble ();
        Long num = Dis.readlong ();
String str = DIS.READUTF ();        Dis.close (); SYSTEM.OUT.PRINTLN (Point + "," + num + "," + str ")/content: 2.5, 100, data type}/** * data + type output to byte array * @throws IOEX
        Ception */public static byte[] Write () throws ioexception{//target array byte[] des = null;
        Double point = 2.5;
        Long num = 100L;
        String str = "data type";
        Select Stream DataOutputStream Bytearrayoutputstream baos = new Bytearrayoutputstream ();
        DataOutputStream dos = new DataOutputStream (new Bufferedoutputstream (BAOs));
        The order in which the operation is written must be consistent with the order of reading to prepare dos.writedouble (point);
        Dos.writelong (num);
        Dos.writeutf (str);//String Dos.flush ();
        des = Baos.tobytearray ();
        Baos.close ();
        Dos.close ();
    Return des; /** * Read data from byte array (basic +string) + type * @throws ioexception/public static void Read (byte[) src) throw s ioexception{//select stream DataInputStream dis = New DataInputStream (New Bufferedinputstream (SRC));
        The order of operation Read is consistent with writing and must exist in order to read//not consistent, data exists double point = dis.readdouble ();
        Long num = Dis.readlong ();
        String str = DIS.READUTF ();
        Dis.close (); SYSTEM.OUT.PRINTLN (Point + "," + num + "," + str ");/content: 2.5, 100, data type}}

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.