32、IO流——字节流FileInputStream、FileOutputStream类

本文详细介绍了Java中的IO流,包括输入流、输出流、字节流和字符流的基本概念和常用类。具体讲解了FileInputStream、FileOutputStream的使用方法,如字节读写、文件复制,并引入了BufferedOutputStream以提高效率。此外,还提到了不同操作系统下的换行符差异。

定义

实现了设备之间的数据信息传输

分类

流向:
输入流:读取数据到代码里
输出流:写入数据到文件里

数据类型:
字节流:
字节输入流:类:InputStream
字节输出流:类:OutputStream
字符流:
字符输入流:类:Reader
字符输出流:类:Writer
I/O流,没有明确说明按哪种分类的话,默认按照数据类型来分
不知道使用什么流的时候使用字节流,字节流早出所以功能多
注意:
每种基类的子类都是一父类名作为后缀
XXXXOutputStream
XXXXInputStream
XXXXReader
XXXXWriter
读操作需要现有文件,写操作不需要

体系结构:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

FileOutputStream

构造方法
public FileOutputStream(String name) throws FileNotFoundException
创建一个向具有指定名称的文件中写入数据的输出文件流,创建一个新 FileDescriptor 对象来表示此文件连接

成员方法
void write(byte[] b)将 b.length 个字节从指定 byte 数组写入此文件输出流中

void write(int b)将指定字节写入此文件输出流

void write(byte[] b, int off, int len)写入一个字节数组的一部分

换行符:
换行符是和系统有关的
Windows 系统的换行符是:\r\n
高级文本编辑器可以自动识别任意的换行符号
Windows 系统自带的记事本不可以,必须\r\n
Linux 系统的换行符是:\n
Mac 系统的换行符是:\r

使用:
转换数组读取:

FileOutputStream fos1 = new FileOutputStream("1.txt");
String string="I love Java";
byte[] bs=string.getBytes();
fos1.write(bs);
fos1.close();

放入数组读取:

FileOutputStream fos1 = new FileOutputStream("fos.txt");
fos1.write(97);
fos1.write(98);

byte[] bs={97,98,99};
fos1.write(bs);
fos1.close();

继续添加读取:

FileOutputStream fos1 = new FileOutputStream("fos.txt",true);
for (int i = 0; i < 5; i++) {
	fos1.write(("nihao"+i).getBytes());
	fos1.write("\r\n".getBytes());
}
fos1.close();

try…catch形式:

// 初始化值
FileOutputStream fos1=null;
try {
	// 赋值
 fos1 = new FileOutputStream("fos.txt");
 // 写入数据
	fos1.write("I love java".getBytes());
} catch (FileNotFoundException e) {
	e.printStackTrace();
}catch (IOException e) {
	e.printStackTrace();
} finally {
 // fileOutputStream有可能为null:
	// 第20行出错的话,进入第25行,然后此时fileOutputStream就为null;
	if (fos1!=null) {
		try {
   // 记得close
			fos1.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

FileInputStream

构造方法
public FileInputStream(String name) throws FileNotFoundException
通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定,创建一个新 FileDescriptor 对象来表示此文件连接

成员方法
public int read() throws IOException
从此输入流中读取一个数据字节,如果没有输入可用,则此方法将阻塞
指定者:类 InputStream 中的 read
返回:下一个数据字节;如果已到达文件末尾,则返回 -1

使用
一个字节一个字节读取:

FileInputStream fis1 = new FileInputStream("2.txt");
int fileRead= 0;
while ((fileRead = fis1.read()) != -1) {
	System.out.println((char)fileRead);
}
fis1.close();

数组读取:

FileInputStream fis1 = new FileInputStream("3.txt");
byte[] bs=new byte[5];
int length=0;
while ((length=fis1.read(bs))!=-1) {
	System.out.println(new String(bs,0,length));
}

复制内容:

FileInputStream fis1 = new FileInputStream("3.txt");
FileOutputStream fos1 = new FileOutputStream("4.txt");
int num=0;
while ((num=fis1.read())!=-1) {
	fos1.write(num);
}
fis1.close();
fos1.close();

复制视频:
形式一:

FileInputStream fileInputStream= 
	new FileInputStream("D:\\QiuBo\\Class\\LessonPlan\\1220\\Video\\Collection使用哪个实现类分析.mp4");
FileOutputStream fileOutputStream= 
	new FileOutputStream("D:\\QiuBo\\Class\\LessonPlan\\IOStudy22222\\Collection使用哪个实现类分析.mp4");

// 复制数据
int by = 0;
// 读取数据
while ((by = fileInputStream.read()) != -1) {
	// 写入数据
	fileOutputStream.write(by);
}
System.out.println("总耗时毫秒:"+ (System.currentTimeMillis() - startTime));
// 释放资源
fileInputStream.close();
fileOutputStream.close();

形式二:

FileInputStream fileInputStream= 
	new FileInputStream("D:\\QiuBo\\Class\\LessonPlan\\1220\\Video\\Collection使用哪个实现类分析.mp4");
FileOutputStream fileOutputStream= 
	new FileOutputStream("D:\\QiuBo\\Class\\LessonPlan\\IOStudy22222\\Collection使用哪个实现类分析.mp4");
// 复制数据
byte[] bys = new byte[1024];//1024bytes = 1KB
int length = 0;
// 读取数据
while ((length=fileInputStream.read(bys)) != -1) {
	// 写入数据
	fileOutputStream.write(bys,0,length);
}
// 释放资源
fileInputStream.close();
fileOutputStream.close();

BufferedOutputStream

JAVA提供了带有缓冲功能的IO高效类,
该类实现缓冲的输出流。通过设置这种输出流,应用程序就可以将各个字节写入底层输出流中,而不必针对每次字节写入调用底层系统

构造方法
public BufferedOutputStream(OutputStream out)
创建一个新的缓冲输出流,以将数据写入指定的底层输出流

成员方法
拷贝文件
使用FileInputStream和fileOutputStream类
使用字节数据流拷贝任意类型的文件

使用

//创建BufferedOutputStream对象
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("bos.txt"));
//写入数据
bufferedOutputStream.write("hahaha".getBytes());
//释放资源
bufferedOutputStream.close();

public class BufferedInputStream extends FilterInputStream  

BufferedInputStream

为另一个输入流添加一些功能,即缓冲输入以及支持 mark 和 reset 方法的能力

构造方法
public BufferedInputStream(InputStream in)
创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用
创建一个内部缓冲区数组并将其存储在 buf 中

使用

BufferedInputStream bis = 
	new BufferedInputStream(new FileInputStream("4.txt"));

int length=0;
while ((length=bis.read())!=-1) {
	System.out.print((char)length);
}
		
byte[] bs=new byte[1024];
int length=0;
while ((length=bis.read(bs))!=-1) {
	System.out.println(new String(bs,0,length));
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值