Input/output stream-byte stream, input/output byte stream

Source: Internet
Author: User

Input/output stream-byte stream, input/output byte stream

It is appropriate to see stream operations after a file.

Conjecture that the write and write operations on data are essentially binary numbers in the form of 01, and the arrangement of these data cannot be messy. They are an ordered whole, and the long lines of content are called streams. Stream is used for identification.

Multiple stream classes are defined in the java. io package to implement the input/output function. They can be classified:

1. Data streams are divided into input and output directions.

2. Data is processed in byte streams and byte streams.

3. Different functions are divided into node streams and processing streams.

Package IOPart; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. IOException; public class IOPart {public static void main (String [] args) {/*** in the F: \ tryfiledirectory, create an a.txt file and modify the content: used to try the first demo of inputStream. */try {// if such a file cannot be found in this path, an exception is reported, therefore, use FileNotFoundException to wrap FileInputStream fileInputStream = new FileInputStream (new File ("f: \ tryFile \ a.txt"); byte [] contents = new byte [1024]; fileInputStream. read (contents); // a read/write exception is reported here. When data is written from the file stream to the array in the memory, a problem may occur, use IOException to wrap String result = new String (contents); System. out. println (result);} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();}}}

Running result:

Used to try the first demo of inputStream.

// This editor is really advanced. It will automatically erase many spaces behind me. As a matter of fact, we have created an array of up to 1024 bite bits. So. Fill in the blanks and make up the number of reads. In this way, there will be drawbacks. The size of the data we want to store is uncertain. How big is this byte suitable?

Solution 1:

Package IOPart; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. IOException; public class IOPart {public static void main (String [] args) {/*** in the F: \ tryfiledirectory, create an a.txt file and modify the content: used to try the first demo of inputStream. */try {// if such a file cannot be found in this path, an exception is reported, therefore, use FileNotFoundException to wrap FileInputStream fileInputStream = new FileInputStream (new File ("f: \ tryFile \ a.txt"); byte [] contents = new byte [fileInputStream. available ()]; fileInputStream. read (contents); // a read/write exception is reported here. When data is written from the file stream to the array in the memory, a problem may occur, use IOException to wrap String result = new String (contents); System. out. println (result);} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();}}}

A stream must be closed after being used as a resource.

Package IOPart; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. IOException; public class IOPart {public static void main (String [] args) {/*** in the F: \ tryfiledirectory, create an a.txt file and modify the content: used to try the first demo of inputStream. */FileInputStream fileInputStream = null; try {// if such a file cannot be found in this path, it will be reported. The file has not found an exception, so fileIn must be wrapped in FileNotFoundException. PutStream = new FileInputStream (new File ("f: \ tryFile \ a.txt"); byte [] contents = new byte [fileInputStream. available ()]; fileInputStream. read (contents); // a read/write exception is reported here. When data is written from the file stream to the array in the memory, a problem may occur, use IOException to wrap String result = new String (contents); System. out. println (result);} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} finally {if (f IleInputStream! = Null) {try {fileInputStream. close () ;}catch (IOException e) {e. printStackTrace ();}}}}}

This extraction form is important.

Method 2:

Package IOPart; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. IOException; public class IOPart {public static void main (String [] args) {IOPart ioPart = new IOPart (); ioPart. method2 ();} private void method2 () {FileInputStream fileInputStream = null; try {fileInputStream = new FileInputStream (new File ("f: \ tryFile \ SendPart. java "); byte [] co Ntents = new byte [1024]; int length = 0; while (length = fileInputStream. read (contents ))! =-1) {System. out. println (new String (contents, 0, length) ;}} catch (FileNotFoundException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();} finally {if (fileInputStream! = Null) {try {fileInputStream. close ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace () ;}}} private void method1 () {/*** in the F: \ tryfiledirectory, create an a.txt file and change the file content to used to try the first demo of inputStream. */FileInputStream fileInputStream = null; try {// if such a file cannot be found in this path, an exception is reported, therefore, use FileNotFoundException to wrap fileInputStream = new FileInputStream (new Fil E ("f: \ tryFile \ a.txt"); byte [] contents = new byte [fileInputStream. available ()]; fileInputStream. read (contents); // a read/write exception is reported here. When data is written from the file stream to the array in the memory, a problem may occur, use IOException to wrap String result = new String (contents); System. out. println (result);} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} finally {if (fileInputStream! = Null) {try {fileInputStream. close () ;}catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();}}}}}

Write:

Package IOPart; import java. io. file; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; public class OutputDemo1 {public static void main (String [] args) {FileOutputStream fileOutputStream = null; try {fileOutputStream = new FileOutputStream (new File ("F: \ tryFile \ output.txt "); String contentString = new String (" lifei "); fileOutputStream. write (contentSt Ring. getBytes (); System. out. println ("written successfully");} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();} finally {if (fileOutputStream! = Null) {try {fileOutputStream. close () ;}catch (IOException e) {e. printStackTrace ();}}}}}

Byte stream copy file:

Package IOPart; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; public class CopyFile1 {public static void main (String [] args) {method2 ();} private static void method2 () {FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try {// C :\\ Users \ Administrator \ Desktop \ shock the world The speech "dream" SD (270 P ). qlv // fileInputStream = new FileInputStream (new File ("f:/tryFile/SendPart. java ");/*** you can also copy video files and audio files, which are actually various files, put it in FileInputStream. Of course, there are also operational stream objects specially designed for characters. */FileInputStream = new FileInputStream (new File ("C :\\ Users \ Administrator \ Desktop \ shocking world speech" dream "SD (270 P ). qlv "); fileOutputStream = new FileOutputStream (new File (" f:/tryFile/copy. qlv "); byte [] contents = new byte [1024]; int length = 0; while (length = fileInputStream. read (contents ))! =-1) {fileOutputStream. write (contents, 0, length) ;}} catch (FileNotFoundException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();} finally {if (fileInputStream! = Null) {try {fileInputStream. close () ;}catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} if (fileOutputStream! = Null) {try {fileOutputStream. close ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} System. out. println ("copied");} private static void method1 () {FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try {fileInputStream = new FileInputStream (new File ("f: /tryFile/SendPart. java "); fileOutputStream = new FileOutputStream (new Fi Le ("f:/tryFile/copy.txt"); byte [] contents = new byte [fileInputStream. available ()]; fileInputStream. read (contents); fileOutputStream. write (contents);} catch (FileNotFoundException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();} finally {if (fileInputStream! = Null) {try {fileInputStream. close () ;}catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} if (fileOutputStream! = Null) {try {fileOutputStream. close ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} System. out. println ("copied ");}}

 

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.