流对象 java_Java基础-IO流对象之字节流(Stream)

Java基础-IO流对象之字节流(Stream)

作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

在前面我分享的笔记中,我们一直都是在操作文件或者文件夹,并没有给文件中写任何数据。现在我们就要开始给文件中写数据,或者读取文件中的数据。什么是输入呢?我们这里的输入指的是将文件的内容加载到程序中的过程叫做输入,那上面叫做输出呢?就是将程序的内容持久化到硬盘上叫做输出。

一.字节输出流(outputStream)

java.io.OutputStream此抽象类是表示输出字节流的所有类的超类。

作用:从Java程序将内存中的数据写入到磁盘文件中。

1>.字节输出流FileOutputStream写字节

1 /*

2 @author :yinzhengjie3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/

4 EMAIL:y1053419035@qq.com5 */

6

7 packagecn.org.yinzhengjie.note5;8

9 importjava.io.FileOutputStream;10 importjava.io.IOException;11

12 public classFileOutputStreamDemo {13 public static void main(String[] args) throwsIOException {14 //流对象的构造方法,可以根据路径创建文件,如果文件存在则直接清空文件内容!

15 FileOutputStream fos = new FileOutputStream("yinzhengjie.txt");16 //往文件中写一个字节

17 fos.write(50);18 fos.write(48);19 fos.write(49);20 fos.write(56);21 //释放资源

22 fos.close();23 }24 }

74e308e147aea0adae1f54846e06f5d5.png

2>.字节输出流FileOutputStream写字节数组

1 /*

2 @author :yinzhengjie3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/

4 EMAIL:y1053419035@qq.com5 */

6

7 packagecn.org.yinzhengjie.note5;8

9 importjava.io.FileOutputStream;10 importjava.io.IOException;11

12 public classFileOutputStreamDemo {13 public static void main(String[] args) throwsIOException {14 //流对象的构造方法,可以根据路径创建文件,如果文件存在则直接清空文件内容!

15 FileOutputStream fos = new FileOutputStream("yinzhengjie.txt");16 //写一个字节数组

17 byte[] bytes = {65,66,67,68,69,70};18 fos.write(bytes);19 //释放资源

20 fos.close();21 }22 }

438a7fa752fa7cd296402a15cd2d427c.png

3>.字节输出流FileOutputStream写字节数组的一部分

1 /*

2 @author :yinzhengjie3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/

4 EMAIL:y1053419035@qq.com5 */

6

7 packagecn.org.yinzhengjie.note5;8

9 importjava.io.FileOutputStream;10 importjava.io.IOException;11

12 public classFileOutputStreamDemo {13 public static void main(String[] args) throwsIOException {14 //流对象的构造方法,可以根据路径创建文件,如果文件存在则直接清空文件内容!

15 FileOutputStream fos = new FileOutputStream("yinzhengjie.txt");16 byte[] bytes = {65,66,67,68,69,70};17 //写一个字节数组的一部分,需要传入数组对象,数组的开始索引,从开始索引开始计算需要写入的长度

18 fos.write(bytes,0,3);19 //释放资源

20 fos.close();21 }22 }

d24288b314072ad001a8006858223733.png

4>.写入字节数组的简便方式(写入字符串)

1 /*

2 @author :yinzhengjie3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/

4 EMAIL:y1053419035@qq.com5 */

6

7 packagecn.org.yinzhengjie.note5;8

9 importjava.io.FileOutputStream;10 importjava.io.IOException;11

12 public classFileOutputStreamDemo {13 public static void main(String[] args) throwsIOException {14 //流对象的构造方法,可以根据路径创建文件,如果文件存在则直接清空文件内容!

15 FileOutputStream fos = new FileOutputStream("yinzhengjie.txt");16 //写入字符串数组

17 fos.write("yinzhengjie".getBytes());18 fos.close();19 }20 }

91c718f023fa364db37cc034113ba416.png

5>.以追加的方式写入

1 /*

2 @author :yinzhengjie3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/

4 EMAIL:y1053419035@qq.com5 */

6

7 packagecn.org.yinzhengjie.note5;8

9 importjava.io.File;10 importjava.io.FileOutputStream;11 importjava.io.IOException;12

13 public classFileOutputStreamDemo {14 public static void main(String[] args) throwsIOException {15 File file = new File("yinzhengjie.txt");16 //以追加的方式写入数据,需要在FileOutputStream的构造方法中指定参数为true.

17 FileOutputStream fos = new FileOutputStream(file,true);18 //写入换行符

19 fos.write("\r\n".getBytes());20 fos.write("yinzhengjie\r\n".getBytes());21 fos.close();22 }23 }

0fc3d1792688121318cd2c9622312207.png

二.IO中的异常处理

1 /*

2 @author :yinzhengjie3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/

4 EMAIL:y1053419035@qq.com5 */

6

7 packagecn.org.yinzhengjie.note5;8

9 importjava.io.FileOutputStream;10 importjava.io.IOException;11

12 public classFileOutputStreamDemo {13 public static voidmain(String[] args) {14 //try 外面声明变量,try里面建立对象,也就是将fos的作用域提示,这样就可以让finally作用域可以访问到。

15 FileOutputStream fos = null;16

17 try{18 //注意,这里给的盘符如果不存在的话就会创建失败,此时fos的值就位null。

19 fos = new FileOutputStream("D:\\10.Java\\JavaSE\\eclipse\\Myprogram\\workspace\\Day8\\yinzhengjie.txt");20 fos.write("yinzhengjie".getBytes());21

22 } catch(IOException e) {23 //如果硬件问题应该让用户重试,比如用户在写入数据的时候拔掉U盘。

24 System.out.println(e.getMessage());25 throw new RuntimeException("文件写入失败,请重试!");26 }finally{27 try{28 //在释放资源的时候需要对流对象进行判断是否为null,如果不是null。表示对象建立成功,需要关闭资源。

29 if(fos!=null) {30 fos.close();31 }32 } catch(IOException e) {33 throw new RuntimeException("关闭资源失败!");34 }35 }36 }37 }

三.字节输入流InputStream

java.io.InputStream此抽象类是表示输出字节流的所有类的超类。

作用:将磁盘文件文件内容加载到内存中来。

1>.按照一个字节进行读取

1 /*

2 @author :yinzhengjie3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/

4 EMAIL:y1053419035@qq.com5 */

6

7 packagecn.org.yinzhengjie.note5;8

9 importjava.io.File;10 importjava.io.FileInputStream;11 importjava.io.FileNotFoundException;12 importjava.io.IOException;13

14 public classFileInputStreamDemo {15 public static void main(String[] args) throwsIOException {16 File file = new File("D:\\10.Java\\JavaSE\\eclipse\\Myprogram\\workspace\\Day8\\yinzhengjie.txt");17 FileInputStream fis = newFileInputStream(file);18 int index = 0;19 //依次读取一个字节,当读到文件末尾时index的值为-1,因此称为结束循环的条件。

20 while((index = fis.read()) != -1) {21 System.out.print((char)index);22 }23 fis.close();24 }25 }26

27 /*

28 以上代码执行结果如下:29 yinzhengjie30 Java31 201832 Bg Date33 golang34 Python35 Linux36 shell37 */

2>.按照一个字节数组进行读取数据

1 /*

2 @author :yinzhengjie3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/

4 EMAIL:y1053419035@qq.com5 */

6

7 packagecn.org.yinzhengjie.note5;8

9 importjava.io.File;10 importjava.io.FileInputStream;11 importjava.io.FileNotFoundException;12 importjava.io.IOException;13

14 public classFileInputStreamDemo {15 public static void main(String[] args) throwsIOException {16 File file = new File("D:\\10.Java\\JavaSE\\eclipse\\Myprogram\\workspace\\Day8\\yinzhengjie.txt");17 FileInputStream fis = newFileInputStream(file);18 //创建字节数组

19 byte[] buf = new byte[4096];20 intindex ;21 //依次读取一个字节,当读到文件末尾时index的值为-1,因此称为结束循环的条件。

22 while((index = fis.read(buf)) != -1) {23 //调用字符串的构造方法,将读取的数据转换成字符串

24 System.out.print(new String(buf,0,index));25 }26 fis.close();27 }28 }29

30 /*

31 以上代码执行结果如下:32 yinzhengjie33 Java34 201835 Bg Date36 golang37 Python38 Linux39 shell40 */

四.小试牛刀

1>.字节流复制文件读取单个字节

1 /*

2 @author :yinzhengjie3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/

4 EMAIL:y1053419035@qq.com5 */

6

7 packagecn.org.yinzhengjie.note5;8

9 importjava.io.File;10 importjava.io.FileInputStream;11 importjava.io.FileOutputStream;12 importjava.io.IOException;13

14 public classFileCopyDemo {15 public static void main(String[] args) throwsIOException {16

17

18 FileInputStream fis = null;19 FileOutputStream fos = null;20

21 try{22 File src = new File("D:\\10.Java\\JavaSE\\eclipse\\Myprogram\\workspace\\Day8\\yinzhengjie.txt");23 File dest = new File("D:\\10.Java\\JavaSE\\eclipse\\Myprogram\\workspace\\Day8\\yinzhengjie.backup");24 //建立两个流的对象,绑定源文件和目标文件

25 fis = newFileInputStream(src);26 fos = newFileOutputStream(dest);27 //字节输入流,读取一个字节,输出流写一个字节

28 intindex ;29 while((index = fis.read()) != -1) {30 fos.write(index);31 }32 }catch(IOException e) {33 System.out.println(e.getMessage());34 throw new RuntimeException("文件复制失败");35 }finally{36 try{37 if(fos != null) {38 fos.close();39 }40 }catch(IOException e) {41 throw new RuntimeException("是否资源失败");42 }finally{43 try{44 if(fis != null) {45 fis.close();46 }47 }catch(IOException e) {48 throw new RuntimeException("释放资源失败");49 }50 }51 }52 }53 }

2cbce743dccbce45cdef8d476e6f5fb9.png

2>.字节流复制文件读取字节数组

1 /*

2 @author :yinzhengjie3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/

4 EMAIL:y1053419035@qq.com5 */

6

7 packagecn.org.yinzhengjie.note5;8

9 importjava.io.File;10 importjava.io.FileInputStream;11 importjava.io.FileOutputStream;12 importjava.io.IOException;13

14 public classFileCopyDemo {15 public static void main(String[] args) throwsIOException {16 FileInputStream fis = null;17 FileOutputStream fos = null;18

19 try{20 File src = new File("D:\\10.Java\\JavaSE\\eclipse\\Myprogram\\workspace\\Day8\\yinzhengjie.txt");21 File dest = new File("D:\\10.Java\\JavaSE\\eclipse\\Myprogram\\workspace\\Day8\\yinzhengjie.backup2");22 //建立两个流的对象,绑定源文件和目标文件

23 fis = newFileInputStream(src);24 fos = newFileOutputStream(dest);25 //定义字节数组,缓冲数据

26 byte[] buf = new byte[4096];27 //读取数组,写入数组

28 intindex;29 while((index = fis.read(buf)) != -1) {30 fos.write(buf,0,index);31 }32 }catch(IOException e) {33 System.out.println(e.getMessage());34 throw new RuntimeException("文件复制失败");35 }finally{36 try{37 if(fos != null) {38 fos.close();39 }40 }catch(IOException e) {41 throw new RuntimeException("是否资源失败");42 }finally{43 try{44 if(fis != null) {45 fis.close();46 }47 }catch(IOException e) {48 throw new RuntimeException("释放资源失败");49 }50 }51 }52 }53 }

c1958fa7b61f03b7a43360e9789b76e1.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值