节点流FileInputStream的使用
构造方法:
FileInputStream fis = new FileInputStream("文件目录");
FileInputStream fis = new FileInputStream(文件对象);
常用方法
int fis.read() 读取一个字节的数据,并返回int类型,如果返回-1表示文件读完了
int fis.read(byte[] bys) 将读取的内容装进字节数组内,字节数组要提前初始化,一般设置为1024的整数倍,返回的是当次读取到的字节数
节点流FileOutputStream的使用
构造方法:
同上
常用方法
void fos.write(int b) 将指定字节b写入文件
void fos.write(byte[] bys) 将字节数组写入文件
void fos.write(byte[] bys, int off, int len) 将字节数组部分写入文件,并设置偏移量和长度,本方法常用!
文件拷贝
public static void copy(File src,File dest) throws IOException {
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dest);
byte[] bys = new byte[1024]; //自定义缓冲区
int len = 0;
while((len = fis.read(bys))!=-1) {
fos.write(bys,0,len);
}
fos.close();
fis.close();
}
套接流BufferedInputStream的使用
带缓冲区的字节流
构造法方法:
BufferedInputStream bis = new BufferedInputStream(InputStream的实现对象);
BufferedInputStream bis = new BufferedInputStream(InputStream的实现对象, int size); //自定义缓冲区大小,默认是8192字节8KB