Dark Horse programmer--26, basic data manipulation flow, byte array operation flow, conversion stream, encoding table

Source: Internet
Author: User

------<ahref= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------

Dark Horse programmer--26,

Datainputstream,dataoutputstream,

Bytearrayinputstream,bytearrayoutputstream,

Inputstreamreader,outputstreamwriter, Code table

/*

DataInputStream and DataOutputStream

Flows that can manipulate the base data type more

Note that this is data, not date!.

*/

 Import java.io.*;class ioliou31{public static void Main (string[] args) throws IOException {               Utfdemo ();          Method ();  public static void Method () throws IOException {file F=new file ("f:\\ science. txt");  Encapsulates the destination file as an object FileOutputStream fos=new FileOutputStream (f);//stream of the associated destination file DataOutputStream dos=new           DataOutputStream (FOS);           The constructor for the stream that specifically operates the basic type of data is received//the following note is written in the format//different write format is Dos.writeint (56) written according to different digits;           Dos.writeboolean (FALSE);     Dos.writedouble (15.24);          /* */FileInputStream fis=new FileInputStream (f);                 DataInputStream dis=new datainputstream (FIS);           Read the note to be read sequentially, as well as read the format//different reading format is according to different bits to read the int i =dis.readint ();           Boolean Bo=dis.readboolean ();           Double dou=dis.readdouble ();           Soc (i+ "---" +bo+ "---" +dou); Special note: What format to write is usedFormat read out dos.close ();                      Dis.close ();  public static void Utfdemo () throws IOException {file F=new file ("f:\\ science. txt"); Encapsulates the destination file as an object FileOutputStream fos=new FileOutputStream (f);//stream of the associated destination file DataOutputStream Dos=ne            W DataOutputStream (FOS);           Dos.writeutf ("This is");//four bytes consisting of one character, altogether eight bytes FileInputStream fis=new fileinputstream (f);           DataInputStream dis=new datainputstream (FIS);           Soc (Dis.readutf ()); The readUTF method returns a string, what format to use in what format to read} public static void Soc (Object obj) {SYSTEM.O                    Ut.println (obj); }}

———————— Split Line ————————

/*

Manipulating the stream of byte arrays (internally encapsulated byte arrays with variable length)

Bytearrayinputstream

Bytearrayoutputstream

Pass a byte array directly to its Bytearrayinputstream constructor,

The byte array passed in is the source

SOURCE Device:

Keyboard system.in HDD FileStream memory Arraystream

Purpose equipment

Console System.out hard disk FileStream memory Arraystream

These two streams do not invoke the underlying resource without closing the close

In addition, there are bytearrayreader and bytearraywriter usages similar to the above.

The Bytearrayreader constructor passed in a character array.

*/

Import java.io.*; Class  ioliou32{         public   static void Main (string[] args) throws IOException         {                   Bytearrayinputstream   bais=new   bytearrayinputstream ("O O ' ao". getBytes ());                   Bytearrayoutputstream   baos=new  bytearrayoutputstream ();                   int  i=0;                  while ((I=bais.read ())!=-1)                  {                       baos.write (i);                          }                   File   f=new  file ("f:\\ Cola cola. Java");                   FileOutputStream    fos=new FileOutputStream (f);                   Baos.writeto (FOS);    Directly written to the byte output stream                                } public         static void  Soc (Object  obj)         {            System.out.println (obj);}                 }

—————— Split Line ——————

/*

Conversion Stream and encoding table:

Convert Stream InputStreamReader and OutputStreamWriter

Encoding table:

ASCII American Standard Information Interchange code, denoted by a byte of 7 bits

Iso8859-1 European Code table, Latin Code table does not recognize Chinese characters, with a byte 8-bit representation

GB2312 China Chinese Code table

GBK Chinese Code table upgrade, two bytes represents a character

Unicode International standard Code, code table used by Java

UTF-8 up to three bytes to represent one character

*/

Import Java.io.*;class ioliou33{publicstatic void Main (string[] args) throws IOException {//writ          E ();                        Read ();                   public static void Write () throws IOException {file F=new file ("f:\\ building. txt");                   FileOutputStream fos=new FileOutputStream (f);                    OutputStreamWriter osw=new outputstreamwriter (FOS, "UTF-8");//To add the Encoding table version Osw.write ("Hands-on");       Osw.close ();                   /* Although written after we have opened f:\\ building. java files still see characters, but in essence the files store the corresponding numbers on the encoded table, but they are converted into characters according to the specified encoding table.   */} public static void Read () throws IOException {File f=new                   File ("f:\\ building. txt");                   FileInputStream fis=new FileInputStream (f); InputStreamReader isr=new inputstreamreader (FIS, "UTF-8");//The Encoding table version to correspond to/* Read is also in accordance with the stored Number followed by the specifiedThe Code table finds the corresponding character */char[] ch=new char[20];                      int i=0;                             while ((I=isr.read (CH))!=-1) {Soc (new String (ch,0,i));                               } isr.close ();             } public static void Soc (Object obj) {System.out.println (obj); }  }

—————— Split Line ——————

/*

Encoding and decoding:

Common Coding Table Gbk,utf-8,iso8859-1

*/

Import Java.io.*;import java.util.*;class Ioliou34 {public static void main (string[] args) throws Exception         {method2 ();                   } public static void Method () throws Exception {String s= "start Game";                   Byte[] by= s.getbytes ("Utf-8");//Encode SOC according to Utf-8 Code table (Arrays.tostring (by));                   String S2=new string (by, "iso8859-1");//Decoding Soc ("s2=" +s2) according to ISO8859-1 encoding table;                   If the decoding according to different encoding table will lead to error results//If you encounter this situation will need to encode and decode byte[] by2=s2.getbytes ("iso8859-1");                   String S3=new string (by2, "utf-8");                                 Soc ("s3=" +S3);                   } public static void Method2 () throws Exception {String s= "start Game";                   Byte[] by= s.getbytes ("Utf-8");//Encode SOC according to Utf-8 Code table (Arrays.tostring (by)); String s2=new STring (By, "GBK");                   Soc ("s2=" +s2);                   If the decoding according to different encoding table will lead to error results//If you encounter this situation will need to encode and decode byte[] by2= s2.getbytes ("GBK");                   Soc (arrays.tostring (by2));                   String S3=new string (by2, "utf-8");                   Soc ("s3=" +S3); /* Since GBK and Utf-8 are both Chinese encoding tables, so, at this point in the case and method methods, the method of decoding the process error is because the use of the Ios8895-1 Latin Code table, inside Don't know                   Chinese characters, and here method2 the case is the decoding process using the GBK Code table can be recognized, the decoding of the characters will go to GBK Code table garbled area to find, and then use GBK Code table encoding         is not the original number, so, here Method2 method S3 will show garbled, and method methods S3 is displayed correctly.                     */} public static void Soc (Object obj) {System.out.println (obj); }}

—————————— Split Line ——————

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Dark Horse programmer--26, basic data manipulation flow, byte array operation flow, conversion stream, encoding table

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.