java笔记之IO2写操作

本文详细介绍了Java中的IO流概念及其实现方式,包括输入流与输出流的不同应用场景,特别是字符流与字节流的区别和使用场景。通过具体的代码示例展示了如何使用FileOutputStream进行文件的写入操作。

 

IO流的分类

  流向:

    输入流  读出数据

    输出流 写出数据

  数据类型

    字节流

      字节输入流 读取数据 inputstream

      字节输出流 写出数据 outputstream

    字符流

      字符输入流 读取数据 Reader

      字符输出流 写出数据 Write

注意:通常情况按照数据类型来区分

*****需求:向文本文件输出一句话hello world 

  分析:这个时候最好是采用字符流 但是字符流是在字节流之后  故采用的是字节输出流(写一句话)‘

  outputsteam是一个抽象类 所以不能实例化,这样就去寻找子类,因为是文件,然后确定为fileoutputstream(每种基类的子类都是以父类名作为后缀名)

 

FileOutputStream的构造方法:
 *   FileOutputStream(File file)
 *  FileOutputStream(String name)

 

字节输出流操作步骤:
 *   A:创建字节输出流对象
 *   B:写数据
 *   C:释放资源

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

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

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

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

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

 

转载于:https://www.cnblogs.com/lanjianhappy/p/6381261.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值