Java IO stream

Source: Internet
Author: User
Tags closing tag

* IO stream is used to process data transfer between devices
* Java operations on data are streamed through the way
* Java classes for manipulating streams are in IO packages
* Flow is divided into two types: input stream, output stream.
* Flow by operation type is divided into two types:
* Byte stream: The byte stream can manipulate any data, because any data in the computer is stored in bytes
* Character Stream: Character stream can only manipulate pure character data, it is more convenient.

* IO Stream Common parent class
* Abstract parent class for byte stream:
* InputStream
* OutputStream
* Abstract parent class for character streams:
* Reader
* Writer

* IO program Writing
* Before using, import the classes in the IO package
* When using, IO exception handling
* After use, release resources

# # #IO流 (FileInputStream)
* Read () reads one byte at a time
*
FileInputStream fis = new FileInputStream ("Aaa.txt");//Create a file input stream object and associate Aaa.txt
int b;//defines the variable and logs each read to the byte
while ((b = Fis.read ())! =-1) {//assign each read Byte to B and determine if it is-1
System.out.println (b);//Print every byte
}

Fis.close ();//close Stream release resource

# # #IO流 (The Read () method returns a value of what is int)
* The Read () method reads a byte, why it is returned as an int, not a byte
*
because the byte input stream can manipulate any type of file, such as chip audio, these files are stored in binary form, if each read return byte, it is possible to read in the middle of the time encountered 111111111
then this 11111111 is a byte type of-1, our program is encountered-1 will stop the unread, the back of the data is not read, so at the time of reading with the int type received, if 11111111 will be in front of it to fill
24 0 To make up 4 bytes, then 1 of the byte type becomes 255 of the int type so that the entire data is read, and the -1 of the closing tag is the int type
# # #20.04_io Stream (FileOutputStream)
* Write () write one byte at a time
*
FileOutputStream fos = new FileOutputStream ("Bbb.txt");//If there is no bbb.txt, create a
//fos.write (97);//Although an int is written, the first 24 is removed at the time of writing, so a byte is written
fos.write (98);
Fos.write (in);
fos.close ();


# # #IO流 (FileOutputStream append)
* A: Case Presentation
* FileOutputStream Construction method writes out how data is implemented to append data
*
FileOutputStream fos = new FileOutputStream ("Bbb.txt", true);//If there is no bbb.txt, create a
Fos.write (97);//Although an int is written, the first 24 is removed at the time of writing, so a byte is written
Fos.write (98);
Fos.write (99);
Fos.close ();

# # #IO流 (copy picture)
* FileInputStream Read
* FileOutputStream Write

FileInputStream fis = new FileInputStream ("to Youth. mp3");//Create input stream object, associate to youth. mp3
FileOutputStream fos = new FileOutputStream ("Copy.mp3");//create an output stream object, associate Copy.mp3

int b;
while ((b = Fis.read ())! =-1) {
Fos.write (b);
}

Fis.close ();
Fos.close ();

# # #IO流 (copy audio file drawing principle)


* Byte stream reads and writes one bytes at a time copy audio
* Disadvantages: too low efficiency

# # #IO流 (available () method of byte array copy)
* A: Case Presentation
* int read (byte[] b): read one byte array at a time
* Write (byte[] b): Writes out one byte array at a time
* Available () Gets the number of bytes read from the file
* Disadvantages: There may be memory overflow

FileInputStream fis = new FileInputStream ("to Youth. mp3");
FileOutputStream fos = new FileOutputStream ("Copy.mp3");
byte[] arr = new byte[fis.available ()];//make a byte array based on file size
Fis.read (arr);//Read all the bytes on the file into the array
Fos.write (arr);//Writes all the bytes in the array once to the file
Fis.close ();
Fos.close ();

# # #IO流 (define decimal groups)
* Write (byte[] b)
* Write (byte[] b, int off, int len) write a valid number of bytes


# # #IO流 (standard format for defining decimal groups)
* A: Case Presentation
* Byte stream once read and write a bytes array copy pictures and videos
FileInputStream fis = new FileInputStream ("to Youth. mp3");
FileOutputStream fos = new FileOutputStream ("Copy.mp3");
int len;
byte[] arr = new byte[1024 * 8];//custom byte array

While (len = Fis.read (arr))! =-1) {
//fos.write (arr);
fos.write (arr, 0, Len);//write out the byte array to write out the number of valid bytes
}

fis.close ();
fos.close ();

Java IO 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.