IO:指的是设备之间进行数据传入的流
按照流向分类:
输入流
输出流
按照数据类型分类:
字节流
A:字节输入流 InputStream
FileInputStream:
FileInputStream fis = new FileInputStream("读取数据的文件名");
字节缓冲输入流:BufferedInputStream
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("读取数据的文件名"));
//注意:这里的文件必须提前存在
字节输入流读取数据两种方式:
1、一次读取一个字节
int b = 0;
while((b=bis.read())!=-1){
System.out.print((char)b);
}
bis.close();
2、一次读取一个字节数组
byte[] bytes = new byte[1024];
int length = 0;
while((length = bis.read(bytes))!=-1){
System.out.print(new String(bytes,0,length));
}
bis.close();
package com.shujia.wyh.day23;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
public class BufferedInputStreamDemo1 {
public static void main(String[] args) throws Exception {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("g.txt"));
//读取数据
//一次读取一个字节
// int b = 0;
// while ((b=bis.read())!=-1){
// System.out.print((char) b);
// }
System.out.println("======================");
byte[] bytes = new byte[1024];
int length = 0;
while ((length = bis.read(bytes)) != -1) { //bis.read(bytes)调用这个方法,将实际获取到的字节存储到数组中,返回的是数组实际读取到的字节数
System.out.println(new String(bytes, 0, length));
}
//释放资源
bis.close();
}
}
B: 字节输出流 OutputStream
FileOutputStream:
FileOutputStream fos = new FileOutputStream("写文件的名字");
字节缓冲输出流:BufferedOutputStream
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(写文件的名字));
//注意:这里文件,可以不存在,之后会自动创建
写数据的方式:bos.write()
1、一次写一个字节
2、一次写一个字节数组
3、一次写一个字节数组的一部分
package com.shujia.wyh.day23;
import java.io.Buffered