What are byte streams?

Source: Internet
Author: User

What are byte streams?

1 package com. test; 2 3 import java. io. file; 4 import java. io. fileInputStream; 5 import java. io. fileNotFoundException; 6 import java. io. fileOutputStream; 7 import java. io. IOException; 8 import java. io. inputStream; 9 import java. io. outputStream; 10/** 11 * a stream is a set of sequential bytes with a starting point and a key point. It is a general term and abstract for data transmission. And data transmission between two devices is called a stream. 12 * the essence of a stream is data transmission. Based on the data transmission characteristics, the stream is abstracted into various types for more intuitive data operations. 13 * Data Processing type: byte stream and byte stream 14 * different flows: input stream and output stream 15 * @ author lcdn16 * 17 */18 public class TestByteSream {19/** 20 * bytes input stream: read data from a file into the program --- read 21 * InputStream class: this abstract class indicates the super class 22 of all classes of the byte input stream * obtain the input byte from a file in the file system using FileInputStream23 */24 public static void read () {25 File file = new File ("e: // B .txt"); 26 try {27 // create an input stream for the file 28 InputStream in = new FileInputStream (File ); 29 byte [] bytes = new byte [1024*1024]; // defines a byte array 30 int len =- 1; // the actual length of each read is 31 StringBuffer buf = new StringBuffer (); 32 while (len = in. read (bytes ))! =-1) {// indicates that there is readable data 33 buf. append (new String (bytes, 0, len); 34} 35 in. close (); 36 System. out. println (buf); 37} catch (FileNotFoundException e) {38 // TODO Auto-generated catch block39 e. printStackTrace (); 40} catch (IOException e) {41 // TODO Auto-generated catch block42 e. printStackTrace (); 43} 44} 45/** 46 * bytes output stream: output data from a program to a file --- write 47 * OutputStream class: this abstract class is the superclass of all classes that output byte streams. The output stream receives the output bytes and sends these bytes 48 * to a receiver of the InoutStream class.. 49 * output to the File using FileOutputStream50 */51 public static void write () {52 file File = new File ("e: // B .txt"); 53 if (! File. exists () {54 try {// file may not be written 55 file. createNewFile (); 56} catch (IOException e) {57 // TODO Auto-generated catch block58 e. printStackTrace (); 59} 60} else {61 try {62/*** 63 * Create an output stream for the file 64 * FileOutStream constructor. The second parameter "true" indicates that the data is written to the original file. data is not overwritten, 65 */66 OutputStream out = new FileOutputStream (file, true); 67 String info = "Android-based Java Article 2! "; 68 // write 69 out data to the file. write (info. getBytes (); 70 // close output stream 71 out. close (); 72} catch (FileNotFoundException e) {73 // TODO Auto-generated catch block74 e. printStackTrace (); 75} catch (IOException e) {76 // TODO Auto-generated catch block77 e. printStackTrace (); 78} 79} 80} 81 public static void main (String [] args) {82 // write (); 83 read (); 84} 85}

 

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.