Java IO Details (iii)------byte input/output stream

Source: Internet
Author: User

Description of the File class: Http://www.cnblogs.com/ysocean/p/6851878.html

Java IO streams are categorized: http://www.cnblogs.com/ysocean/p/6854098.html

So this blog we are talking about byte input and output stream:InputStream, Outputsteam(inside the red rectangle), the Red oval box is its typical implementation (Fileinputsteam, Fileoutstream)

  

1, byte output stream: OutputStream

Public abstract class OutputStream extends Object implements Closeable, flushable

This abstract class is a superclass of all classes that represent a byte output stream. The output stream receives the output byte and sends it to a sink.

Method Summary:

  

Here we introduce the typical implementation fileoutputstream of byte output stream OutputStream:

1. Create the target object, and the output stream indicates which file to save the data to. Do not write the letter, the default file is the root directory of the project under the files target = new file ("io" +file.separator+ "a.txt");//2, create a byte output stream object for the file, the second parameter is a Boolean type, true  Indicates that the file that is written after is appended to the data, false to overwrite outputstream out = new FileOutputStream (target,true),//3, specific IO operation (writes data to file a.txt)/** * void Write (int b): Writes a byte to the file * void Write (byte[] b): Writes all bytes in array b to the file * void Write (byte[] b,int off,int len): The off index in array B is opened The beginning Len bytes are written to the file */out.write (65); Writes A to the file Out.write ("Aa". GetBytes ()); Writes Aa to the file Out.write ("ABCDEFG". GetBytes (), 1, 5); Write bcdef to the file//through the above operation, the data in the A.txt file is AAABCDEF//4, close the stream resource out.close (); System.out.println (Target.getabsolutepath ());

  

2, byte input stream: InputStream

Public abstract class InputStream extends Object implements Closeable

This abstract class is a superclass of all classes that represent input byte streams.

Method Summary:

  

Here we introduce the typical implementation fileinputstream of byte output stream InputStream:

1, create the target object, the input stream represents that file data saved to the program. Do not write the letter, the default file is in the root directory of the project//a.txt saved file content is: aaabcdeffile target = new file ("io" +file.separator+ "a.txt");//2, Create an input stream object InputStream in = new FileInputStream (target),//3, specific IO operation (reading data from A.txt file into the program)/** * Note: Read the data in the file, read the last no data, return 1 * I NT Read (): Reads a byte, returns the read bytes * int read (byte[] b): reads multiple bytes and saves it to array B, starting from the position of the index of array b at 0, returning read several bytes * int read (byte[] B,int off int len): reads multiple bytes and stores it in array B, starting at the position of Array B at index 0, len bytes *///int read (): Reads a byte, returns the read byte int data1 = In.read ();//Gets the A.txt file The first byte of the Data System.out.println ((char) data1); A//int Read (byte[] b): reads multiple bytes saved to array b byte[] buffer = new Byte[10];in.read (buffer),//Gets the first 10 bytes in the A.txt file and stores it in the buffer array SYSTEM.OUT.PRINTLN (arrays.tostring (buffer)); [N, N, a, 0, 0, 0]system.out.println (new String (buffer)); Aabcdef[][][]//int Read (byte[] b,int off,int len): reads multiple bytes and stores it in array B, starting from index off to lenin.read (buffer, 0, 3); SYSTEM.OUT.PRINTLN (arrays.tostring (buffer)); [0, 0, 0, 0, 0, 0, 0]system.out.println (new String(buffer)); AAB[][][][][][][]//4, Close stream resource in.close ();

  

3. Complete file copy with byte stream

  

/** * Copy a.txt files to B.txt *///1, create source and destination file Srcfile = new file ("io" +file.separator+ "a.txt"); File Descfile = new file ("io" +file.separator+ "b.txt");//2, create input/output stream object InputStream in = new FileInputStream (srcfile); O Utputstream out = new FileOutputStream (descfile);//3, read and write operations byte[] buffer = new byte[10];//creates a byte array with a capacity of 10, storing the data that has been read int Len = -1;//indicates how many bytes have been read, and if 1, indicates that the file has been read to the end of the while ((Len=in.read (buffer))!=-1) {//print read Data System.out.println (new String (Buffer,0,len)); /The data in the buffer array starting with 0, Len length is read into the B.txt file out.write (buffer, 0, Len);} 4. Close stream resource out.close (); In.close ();

  

Java IO Details (iii)------byte input/output stream

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.