码迷,mamicode.com
首页 > 其他好文 > 详细

IO流

时间:2018-11-04 21:06:52      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:垃圾回收器   reader   二进制   for   操作   文件   str   span   input   

* 注意:每种基类的子类都是以父类名作为后缀名。
* XxxOutputStream
* XxxInputStream
* XxxReader
* XxxWriter
* 查看FileOutputStream的构造方法:
*
* FileOutputStream(File file)
* FileOutputStream(String name)
*
* 字节输出流操作步骤:
* A:创建字节输出流对象
* B:写数据
* C:释放资源

 1         // 创建字节输出流对象
 2         // FileOutputStream(File file)
 3         // File file = new File("fos.txt");
 4         // FileOutputStream fos = new FileOutputStream(file);
 5         // FileOutputStream(String name)
 6         FileOutputStream fos = new FileOutputStream("fos.txt");
 7         /*
 8          * 创建字节输出流对象了做了几件事情:
 9          * A:调用系统功能去创建文件
10          * B:创建fos对象
11          * C:把fos对象指向这个文件
12          */
13         
14         //写数据
15         fos.write("hello,IO".getBytes());
16         fos.write("java".getBytes());
17         
18         //释放资源
19         //关闭此文件输出流并释放与此流有关的所有系统资源。
20         fos.close();
21         /*
22          * 为什么一定要close()呢?
23          * A:让流对象变成垃圾,这样就可以被垃圾回收器回收了
24          * B:通知系统去释放跟该文件相关的资源
25          */
26         //java.io.IOException: Stream Closed
27         //fos.write("java".getBytes());

 

* 字节输出流操作步骤:
* A:创建字节输出流对象
* B:调用write()方法
* C:释放资源
*
* public void write(int b):写一个字节
* public void write(byte[] b):写一个字节数组
* public void write(byte[] b,int off,int len):写一个字节数组的一部分

 1         // 创建字节输出流对象
 2         // OutputStream os = new FileOutputStream("fos2.txt"); // 多态
 3         FileOutputStream fos = new FileOutputStream("fos2.txt");
 4 
 5         // 调用write()方法
 6         //fos.write(97); //97 -- 底层二进制数据    -- 通过记事本打开 -- 找97对应的字符值 -- a
 7         // fos.write(57);
 8         // fos.write(55);
 9         
10         //public void write(byte[] b):写一个字节数组
11         byte[] bys={97,98,99,100,101};
12         fos.write(bys);
13         
14         //public void write(byte[] b,int off,int len):写一个字节数组的一部分
15         fos.write(bys,1,3);
16         
17         //释放资源
18         fos.close();

 

* 如何实现数据的换行?
* 为什么现在没有换行呢?因为你值写了字节数据,并没有写入换行符号。
* 如何实现呢?写入换行符号即可呗。
* 刚才我们看到了有写文本文件打开是可以的,通过windows自带的那个不行,为什么呢?
* 因为不同的系统针对不同的换行符号识别是不一样的?
* windows:\r\n
* linux:\n
* Mac:\r
* 而一些常见的个高级记事本,是可以识别任意换行符号的。
*
* 如何实现数据的追加写入?
* 用构造方法带第二个参数是true的情况即可

 1         // 创建字节输出流对象
 2         // FileOutputStream fos = new FileOutputStream("fos3.txt");
 3         // 创建一个向具有指定 name 的文件中写入数据的输出文件流。如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。
 4         FileOutputStream fos = new FileOutputStream("fos3.txt", true);
 5 
 6         // 写数据
 7         for (int x = 0; x < 10; x++) {
 8             fos.write(("hello" + x).getBytes());
 9             fos.write("\r\n".getBytes());
10         }
11 
12         // 释放资源
13         fos.close();

 

FileOutPutStream写出数据加入异常处理:

 1         // 改进版
 2         // 为了在finally里面能够看到该对象就必须定义到外面,为了访问不出问题,还必须给初始化值
 3         FileOutputStream fos = null;
 4         try {
 5             // fos = new FileOutputStream("z:\\fos4.txt");
 6             fos = new FileOutputStream("fos4.txt");
 7             fos.write("java".getBytes());
 8         } catch (FileNotFoundException e) {
 9             e.printStackTrace();
10         } catch (IOException e) {
11             e.printStackTrace();
12         } finally {
13             // 如果fos不是null,才需要close()
14             if (fos != null) {
15                 // 为了保证close()一定会执行,就放到这里了
16                 try {
17                     fos.close();
18                 } catch (IOException e) {
19                     e.printStackTrace();
20                 }
21             }
22         }

 

* 字节输入流操作步骤:
* A:创建字节输入流对象
* B:调用read()方法读取数据,并把数据显示在控制台
* C:释放资源
*
* 读取数据的方式:
* A:int read():一次读取一个字节
* B:int read(byte[] b):一次读取一个字节数组

 1         FileInputStream fis = new FileInputStream("FileOutputStreamDemo.java");
 2 
 3         // 最终版代码
 4         int by = 0;
 5         // 读取,赋值,判断
 6         while ((by = fis.read()) != -1) {
 7             System.out.print((char) by);
 8         }
 9 
10         // 释放资源
11         fis.close();

 

IO流

标签:垃圾回收器   reader   二进制   for   操作   文件   str   span   input   

原文地址:https://www.cnblogs.com/samuraihuang/p/9905430.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!