文件读取

1.流的分类
a.从方向划分:输入流和输出流
b.从读取和写入的方式:字符流和字节流
Reader ,Writer.
2.字节流
FileInputStream 和 FileOutputStream

代码如下:
public void fileTest(String path,String newPath){
try {
//实例化一个FileInputStream类的对象
FileInputStream fis = new FileInputStream(path);
FileOutputStream fos = new FileOutputStream(newPath);
//获取文件字节的总数
int length = fis.available();
//根据总数定义一个byte数组
byte [] array = new byte[length];

fis.read(array);
fos.write(array);


3.缓冲流
BufferedInputStream 和 BufferedOutputStream

代码如下:
public void bufferedTest(String path,String newPath){
try {
//实例化一个FileInputStream类的对象
FileInputStream fis = new FileInputStream(path);
BufferedInputStream bis = new BufferedInputStream(fis);

FileOutputStream fos = new FileOutputStream(newPath);
BufferedOutputStream bos = new BufferedOutputStream(fos);
//获取文件字节的总数
int length = bis.available();
//根据总数定义一个byte数组
byte [] array = new byte[length];

bis.read(array);
bos.write(array);
bos.flush();



4.对象流
ObjectInputStream 和 ObjectOutputStream

Student stu = new Student();
oos.wirteObject(stu);

class Student implements 序列化接口 {
name;
transient score;//表示将对象写入文件的时候,瞬时的属性是不会写进去的。
}

文件读取是以一个byte读取,而汉字是两个byte,所以在读取汉字的时候要注意。。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值