Java IO byte throttling, character stream operation summary of the two word throttling

Source: Internet
Author: User
Tags int size stub

The previous article mainly introduces the use of file class files and Randomaccessfile classes. Next, I think it is better to divide io into two parts: byte stream and character stream. It's not confusing, it's easy to compare. This article mainly introduces the word throttling.

1, Byte stream

First the family Atlas of the byte stream.


There are two main parts of the word stream: InputStream, OutputStream. InputStream abstract How the application reads data, OutputStream abstract the way the application writes the data.

Note that you will sometimes encounter eof (end of File), indicating that you have read it. Read to-1 indicates that the end of the file is read.

A, InputStream input flow basic operation method

int B=in.read (), reading a byte, unsigned padding to int low eight bits,-1 indicates EOF.
In.read (BYTE[]BUF), reading data to the byte array buf
in.read (byte[]buf,int start,int size), reading data to byte array buf. Then the size length data is stored from the start position of the BUF

b, outputstream output flow This operation method

Out.write (INTB); writes a byte to stream, writes a low 8 bit
out.write (byte[]buf) of B, and writes a BUF byte array to the stream
Out.write (byte[]buf,int Start,int size); The byte array buf writes the byte of size length from the start position to the stream.

C, FileInputStream Concrete implementation of the file read the file

Note:1> A single byte of reading is not an array, and is less efficient when reading large files.

2> batch read, for large file efficiency is high, is also our most common way to read files.

The 3>byte type is 8 bits, the int type is 32 bits, in order to avoid errors in the conversion of the data type. Through 0xFF will be high 24 to clear zero.

4>read () is suitable for single byte read size

D, FileOutputStream implements the method of writing byte data to a file.

The writing of the file output stream:

FileOutputStream  output=new FileOutputStream (file,true); This method indicates that the file, if it does not exist, creates a new file and, if it exists, adds the content later.
fileoutputstream  output=new fileoutputstream (file)//This method indicates that a file does not exist, creates a new file, deletes the file before creating the file if it exists.

After performing Output.write (byte[] buf,int start,int size), be sure to call the Output.flush () method to flush the buffer.

The test code is as follows:

Package com.ll.iofile;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStreamReader;

Import Java.io.OutputStreamWriter; /** * This method is used to test the character stream of the * main use is InputStreamReader character output flow * OutputStreamWriter * @author lulei * * * */public class Testinput streamreaderandoutputstreamwriter {public static void main (string[] args) throws ioexception{//TODO auto-generated
		Method Stub/* This is the first example of a test * is to copy GBK encoded files into utf-8 format files FileInputStream in=new fileinputstream ("Demo\\fileio1.txt"); InputStreamReader isr=new InputStreamReader (in); The code for the default item here is GBK//////////* This is the first example of the test * is to copy GBK encoded files into a utf-8 format file Fileou
		Tputstream out=new FileOutputStream ("Demo\\outputstreamutf8.txt");
		OutputStreamWriter osw=new OutputStreamWriter (out, "utf-8");
		* * FileInputStream in=new FileInputStream ("C:\\users\\administrator\\desktop\\outputstreamutf8.txt"); InputStreamReader isr=new InputStreamReader (in, "UTF8");//This is set to UTF8 encoding FileoutputstreaM out=new fileoutputstream ("Demo\\outputstreamgbk.txt");
		OutputStreamWriter osw=new OutputStreamWriter (out);//The default encoding is GBK int c=0;
		/* This is a single-byte output while ((C=isr.read ())!=-1) {System.out.print ((char) c);
		} * * * char[] buf=new char[20*1024];
		while ((C=isr.read (buf, 0, buf.length))!=-1) {//String Str=new string (BUF);
			System.out.println (str);
			Osw.write (BUF,0,C);
		Osw.flush ();
		} isr.close ();
	Osw.close ();
 }

}

e, data input and output stream

It is an extension of the byte stream. It is used to help facilitate the manipulation of type data.

1> contains DataOutputStream and DataInputStream

2> to the "flow" function of the extension, you can more easily read Int,long, character and other types of data.

Methods of 3> DataOutputStream:

Writeint ()/writedouble ()/writeutf ()

Methods of 4> DataInputStream:

ReadInt ()/readdouble ()/readutf ()

The code is as follows:

Package com.ll.iofile;
Import Java.io.DataInputStream;
Import Java.io.DataOutputStream;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.lang.reflect.Array;

Import Java.util.Arrays; /** * This class is used to test the input/output stream of the data * i.e. DataOutputStream and DataInputStream * @author Lulei */public class TESTDATAOUTPUTSTREAMANDD Atainputstream {public static void main (string[] args) throws ioexception{//TODO auto-generated Method Stub//test D
		Ataoutputstream class String str= "Demo\\wq77.txt"; DataOutputStream dos=new DataOutputStream (new FileOutputStream (str));//Instantiate an object Dos.write (10);//account for one byte dos.write
		
		Int (-10);//write an int-type integer, accounting for 4 bytes dos.writedouble (10.8);//8 Bytes//write string in UTF-8 format Dos.writeutf ("China");//A man is 3 bytes.
		
		Writes the string in UTF-16BE format dos.writechars ("China");//A Chinese character occupies 2 bytes dos.close ();
		The byte ioutils.printhexbybyte (str) of the printout file;
		
		System.out.println (); The instantiated data input stream object DataInputStream dif=new datainputstream (New FileInputStream (str));
		int A=dif.read ();
		System.out.println (a);
		int B=dif.readint ();
		System.out.println (b);
		Double d=dif.readdouble ();
		
		System.out.println (d);
		String S1=dif.readutf ();
		
		System.out.println (S1);
		Byte[] Bytes=new byte[6];
		Dif.read (bytes);
		
	System.out.println (arrays.tostring (bytes));//Convert to string display Dif.close ();
 }

}

F, bufferedinputstream&bufferedoutputstream (input and output stream with buffer)

These two streams, which provide an operation with a buffer in Io, are typically buffered when the file is opened for write or read operations, which improves IO performance. Putting input into a file from an application is equivalent to pouring one cylinder of water into another.

the 1> Fileoutputstream:write method is equivalent to a drop of water passing through.

2> Dataoutputstream:writexxx method will be more convenient, the equivalent of a ladle of water transfer past

3> Bufferedoutputstream:write () method will be more convenient, the equivalent of a ladle scoop first into the bucket, in from the bucket into the cylinder, performance improved.

The code is as follows:

Package com.ll.iofile;
Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.DataInputStream;
Import Java.io.DataOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;

Import java.io.IOException; /* This function is to test the speed of Bufferedoutputstream, FileOutputStream, DataOutputStream * These three classes in the copy file * * * public class Testbufferedo Utputstreamandbufferedinputstream {public static void main (string[] args) throws ioexception{//TODO auto-generated
		Method Stub file File=new file ("The top of the c:\\users\\administrator\\desktop\\ Wave. pdf");
		Long Start1=system.currenttimemillis ();
		Filetocopybybyte (file);
		Long End1=system.currenttimemillis ();
		
		System.out.println (END1-START1);
		Long Start2=system.currenttimemillis ();
		Filetocopy (file);
		Long End2=system.currenttimemillis ();
		
		System.out.println (END2-START2);
		Long Start3=system.currenttimemillis ();
		Datatocopy (file);
		Long End3=system.currenttimemillis (); System.ouT.println (END3-START3);
		Long Start4=system.currenttimemillis ();
		Bufferedfiletocopy (file);
		Long End4=system.currenttimemillis ();
	System.out.println (END4-START4); public static void Filetocopybybyte (file file) throws ioexception{if (!file.exists ()) {System.out.println ("files" +fi
		Le+ "does not exist");
		} if (!file.isfile ()) {System.out.println ("file" +file+ "not a file"); FileInputStream fis=new fileinputstream (file)//instantiate read bytes Stream//instantiate file stream FileOutputStream Fos =new fileoutputstream
		("Demo\\fileiobybyte1.pdf");
		int b=0;
		while ((B=fis.read ())!=-1) {fos.write (b);
		} fis.close ();
	Fos.close (); public static void Filetocopy (file file) throws ioexception{if (!file.exists ()) {System.out.println ("files" +file
		+ "does not exist");
		} if (!file.isfile ()) {System.out.println ("file" +file+ "not a file"); FileInputStream fis=new fileinputstream (file)//instantiate read bytes Stream//instantiate file stream FileOutputStream Fos =new fileoutputstream
		 ("Demo\\fileio1.pdf");
		 int length=0; Byte[] Buf=new byTE[20*1024];
			while ((Length=fis.read (buf, 0, buf.length))!=-1) {fos.write (BUF);
		 Fos.flush ();
		} fis.close ();
	Fos.close (); The public static void Datatocopy (File datefile) throws ioexception{if (!datefile.exists ()) {System.out.println ("
		The document "+datefile+" does not exist);
		} if (!datefile.isfile ()) {System.out.println ("file" +datefile+ "not a file");
		//Instantiate an object that reads the file DataInputStream dis=new datainputstream (New FileInputStream (Datefile));
		Instantiates an object that writes a file DataOutputStream dos=new dataoutputstream (New FileOutputStream ("Demo\\datafile1.pdf"));
		int length=0;
		Byte[] Buf=new byte[20*1024];
		while ((Length=dis.read (buf, 0, buf.length))!=-1) {dos.write (BUF);
		Dos.flush ();
		//Close file Dis.close ();
	Dos.close (); public static void Bufferedfiletocopy (File bufferedfile) throws ioexception{if (!bufferedfile.exists ()) {Syst
		Em.out.println ("File" +bufferedfile+ "does not exist");
		} if (!bufferedfile.isfile ()) {System.out.println ("file" +bufferedfile+ "not a file"); } BufferEdoutputstream bos=new Bufferedoutputstream (New FileOutputStream ("Demo\\bufferedfile1.pdf"));
		Bufferedinputstream bis=new Bufferedinputstream (New FileInputStream (Bufferedfile));
		int length=0;
		Byte[] Buf=new byte[20*1024];
			while ((Length=bis.read (buf, 0, buf.length))!=-1) {bos.write (BUF);
		Bos.flush ();
		} bis.close ();
	Bos.close ();
 }
}

Here I simply introduced some commonly used word throttling. Like FileInputStream and FileOutputStream. Smart you will know here again is the use of the adorner class design pattern. Haha, the next time someone mentions the decorator design pattern, you say IO stream implementation. I'll introduce the knowledge of character streams in the next 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.