s-day09--使用字节输入流一次读取一个字节
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package com.inmind.inputstream02;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
|
||||
/*
|
||||
8.字节输入流以及相关API的介绍
|
||||
在java中使用抽象类InputStream表示字节输入流
|
||||
常用的子类:FileInputStream
|
||||
构造方法:
|
||||
FileInputStream(File file) 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的 File对象 file命名。
|
||||
FileInputStream(String name) 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name命名。
|
||||
常用方法:
|
||||
void close() 关闭资源
|
||||
int read() 从该输入流读取一个字节的数据。返回值就是字节数据
|
||||
int read(byte[] b) 从字节输入流中将字节数据读取到参数b数组中,返回的值读取的字节个数
|
||||
|
||||
4.字节输入流的基本使用
|
||||
步骤:
|
||||
1.创建对象
|
||||
2.调用读方法
|
||||
3.释放资源
|
||||
|
||||
*/
|
||||
public class Demo06 {
|
||||
public static void main(String[] args) throws Exception{
|
||||
//1.创建对象
|
||||
/*
|
||||
注意:
|
||||
1.创建输入流对象时,文件不存在是报异常java.io.FileNotFoundException: source1.txt
|
||||
2.创建输出流对象时,文件不存在才会新建
|
||||
|
||||
*/
|
||||
FileInputStream fis = new FileInputStream("source1.txt");
|
||||
//2.调用读方法
|
||||
//int read() 从该输入流读取一个字节的数据。返回值就是字节数据
|
||||
/*int read = fis.read();
|
||||
System.out.println((char) read);//97
|
||||
|
||||
read = fis.read();
|
||||
System.out.println((char) read);//98
|
||||
|
||||
read = fis.read();
|
||||
System.out.println((char) read);//99*/
|
||||
|
||||
int c;//用来保存读取到的字节数据
|
||||
while ((c = fis.read()) != -1) {
|
||||
System.out.println((char) c);
|
||||
}
|
||||
|
||||
|
||||
//3.释放资源
|
||||
fis.close();
|
||||
|
||||
System.out.println("程序结束");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user