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