PackageCd.itcast.fileinputstream;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException; Public classDemo1 { Public Static voidMain (string[] args)throwsIOException {//target fileFile File =NewFile ("E:\\a.txt"); //Create a channelFileInputStream FileInputStream =Newfileinputstream (file); //creating a buffered array byte[] buf =New byte[1024]; //reads the data with an array, at which time read () returns, the number of reads, and returns -1 when the read is empty. while(Fileinputstream.read (BUF)!=-1) {System.out.println ("Content:" +NewString (BUF)); } //CloseFileinputstream.close (); }}
Attention:
Finally, the resource Fileinputstream.close () is closed, and if the resource is not freed, the other program cannot manipulate the resource. For example, you cannot delete a resource that is being used.
FileInputStream reading data using buffer arrays