2.2 FileInputStream的用法 (视频下载) (全部书籍)
FileInputStream是InputStream的继承类,从字面上就可看出,它的主要功能就是能从磁盘上读入文件。read方法会一个一个字节的从磁盘往回读数据。
例:2.2.1
import java.io.*;
public class TestMark_to_win {
public static void main(String args[]) throws Exception {
int size;
FileInputStream f1 = new FileInputStream("c:/1.txt");
/*Returns the number of bytes that can be read from this file input
stream without blocking.*/
size = f1.available();
for (int i = 0; i < size; i++) {
/*Reads a byte of data from this input stream. This method blocks
if no input is yet available. Returns: the next byte of data, or
-1 if the end of the file is reached.*/
System.out.println((char) f1.read());
}
int size2;
FileInputStream f2 = new FileInputStream("c:/1.txt");
size2 = f2.available();
for (int i = 0; i < size2; i++) {
/* you use the next statement, only int value can be printed out. */
详情黏贴以下网址在地址栏后请进:
http://www.mark-to-win.com/index.html?content=JavaBeginner/javaUrl.html&chapter=JavaBeginner/JavaBeginner8_web.html#UsageOfFileInputStream