Java Fundamentals Hardening IO Stream Note 21:fileinputstream reading data

Source: Internet
Author: User

1. Operation steps for byte input stream:

(1) Create a byte input stream object

(2) Call the Read () method and display the data to the console

(3) Close the object resource of the byte input stream

2. FileInputStream Construction:

FileInputStream(File file)
Constructs a new FileInputStream based on file .
FileInputStream(FileDescriptor fd)
Constructs a new on the FileInputStream FileDescriptor fd .
FileInputStream(String fileName)
Constructs a new on the FileInputStream file named fileName .

3. code example:

1  PackageCom.himi.fileinputstream;2 3 ImportJava.io.FileInputStream;4 Importjava.io.IOException;5 6 /*7 * How to read data:8 * int Read (): read one byte at a time9 * int read (byte[] b): One reads a byte arrayTen  */ One  Public classFileinputstreamdemo { A  -      Public Static voidMain (string[] args)throwsIOException { -         //FileInputStream (String name) the         //Create a byte input stream object -FileInputStream FIS =NewFileInputStream ("Fis.txt"); -          -          +         //call the Read () method to read the data and display the data in the console -         intby = 0; +          while(by = Fis.read ())! =-1) { ASystem.out.print ((Char) by); at         } -         //Freeing Resources - fis.close (); -  -     } -  in}

The results are as follows:

Java Fundamentals Hardening IO Stream Note 21:fileinputstream reading data

Related 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.