-----------Android Training , Java training , Java Learning Technology blog, look forward to communicating with you!
------------
Dark Horse program Ape--26,
Datainputstream,dataoutputstream,
Bytearrayinputstream,bytearrayoutputstream,
Inputstreamreader,outputstreamwriter, Code table
/*
DataInputStream and DataOutputStream
A stream that can manipulate the basic 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 is specialized in manipulating the basic type of data is received//the following note is written in the format//different write formats are written according to different bits of dos.writeint (56); Dos.writeboolean (FALSE); Dos.writedouble (15.24); /* */FileInputStream fis=new FileInputStream (f); DataInputStream dis=new datainputstream (FIS); Read the note to follow the sequential read, 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 write in what format to read} public static void Soc (Object obj) {System.out.println (obj); }}
———————— Cutting 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, Bytearrayreader and Bytearraywriter are also used in the same way as 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);} }
—————— Cutting 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, denoted by a byte 8 bit
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 number Osw.write ("Hands-on"); Osw.close (); /* Even though we are writing, we will open f:\\ building. java files still see characters. But essentially the file stores the corresponding number on the encoding table, just to convert it into characters according to the specified encoding table. */} public static void Read () throws IOException {file F=new file ("F : \ n Building. txt "); FileInputStream fis=new FileInputStream (f); InputStreamReader isr=new inputstreamreader (FIS, "UTF-8");//Encoding table version number to the corresponding */* reading is also in accordance with the storage The numbers are then referred toIdentification of 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); } }
—————— Cutting Line ——————
/*
Encoding and decoding:
Regular use of the 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 according to iso8859-1 encoding table ("s2=" +s2); It is assumed that decoding according to different coding tables will result in incorrect results//assuming that it is necessary 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=newString (By, "GBK"); Soc ("s2=" +s2); It is assumed that decoding according to different coding tables will result in incorrect results//assuming that it is necessary to encode and decode byte[] by2= s2.getbytes ("GBK") when encountering such a situation; Soc (arrays.tostring (by2)); String S3=new string (by2, "utf-8"); Soc ("s3=" +S3); /* Since both GBK and Utf-8 are Chinese encoding tables, the situation at this point differs from the method approach. The error in the decoding process in method methods is due to the use of the Ios8895-1 Latin Code table, which does not know the Chinese characters, and here method2 is the decoding process using the ability to recognize the GBK code table. Decoding encountered in the number of 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); }}
-----------Android Training , Java training , Java Learning Technology blog, look forward to communicating with you!
------------
Dark Horse Program Ape--26, basic data manipulation flow, byte array operation flow, conversion stream, encoding table