阶段二29_面向对象高级_IO[字节输出流FileOutputStream写数据到文件]

知识点:

  1.字节流写数据的步骤和实现代码。
  2.字节流写数据的3种方式:write(int b),write(byte[] b),write(byte[] b, int off, int len)
  3.字节流写数据如何实现换行
  4.字节流写数据如何实现追加写入
  5.创建字节输出流对象:
    FileOutputStream fos = new FileOutputStream("D:\\a.txt");
    FileOutputStream fos = new FileOutputStream(new File("D:\\a.txt"))
    FileOutputStream fos = new FileOutputStream("bytestream\\a.txt",true);

一.IO流概述

I表示intput,是数据从硬盘进内存的过程,称之为读
 O表示output,是数据从内存到硬盘的过程。称之为写

二.IO流的分类

IO流:

  输入流和输出流

IO流:

  字节流[操作所有类型的文件,包含音频图片等文件]和字符流[只能操作纯文件文件,包含java文件txt文件]

三.字节流

1.字节流写数据的步骤:

  (1)创建字节输出流对象
     注意事项:
       如果文件不存在,就创建。
       如果文件存在就清空。
  (2)写数据
     注意事项:
       写出的整数,实际写出的是整数在码表上对应的字母
  (3)释放资源
     注意事项:
       每次使用完流必须要释放资源。

2.字节流写数据的简单实现:

  public class OutputDemo1 {
    public static void main(String[] args) throws IOException {
        //1.创建字节输出流的对象
        FileOutputStream fos = new FileOutputStream("D:\\a.txt");
        //FileOutputStream fos = new FileOutputStream(new File("D:\\a.txt"));
        //2.写数据
        fos.write(97);
        //3.释放资源
        fos.close();
    }
  }

3.字节流写数据的3种方式

第1种:void write(int b)	一次写一个字节数据
第2种:void write(byte[] b)	一次写一个字节数组数据
第3种:void write(byte[] b, int off, int len)	一次写一个字节数组的部分数据 

具体代码实现:

public class OutputDemo3 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("bytestream\\a.txt");
        //第1种:void write(int b)	一次写一个字节数据
        fos.write(97);
        //第2种:void write(byte[] b)	一次写一个字节数组数据
        byte [] bys1 = {97,98,99};
        fos.write(bys1);
        //第3种:void write(byte[] b, int off, int len)	一次写一个字节数组的部分数据 
        byte [] bys = {97,98,99,100,101,102,103};
        fos.write(bys,1,2);
        fos.close();
    }
}

4.字节流写数据如何实现换行.

  4.1写完数据后,加换行符:
	windows:\r\n
	linux:\n
	mac:\r
  4.2实现案例:
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("bytestream\\a.txt");
        fos.write(97);
        //能加一个换行
        fos.write("\r\n".getBytes());
        fos.close();
    }

5.字节流写数据如何实现追加写入呢

 5.1 实现说明:
    public FileOutputStream(String name,boolean append)
    创建文件输出流以指定的名称写入文件。如果第二个参数为true ,不会清空文件里面的内容
  5.2实现案例:
    public static void main(String[] args) throws IOException {
        //第二个参数就是续写开关,如果没有传递,默认就是false,
        //表示不打开续写功能,那么创建对象的这行代码会清空文件.
        //如果第二个参数为true,表示打开续写功能
        //那么创建对象的这行代码不会清空文件.
        FileOutputStream fos = new FileOutputStream("bytestream\\a.txt",true);
        fos.write(97);
        fos.close();
    }

四.字节流写数据加try…catch…finally异常处理标准流程

 FileOutputStream fos = null;
	try {
		fos = new FileOutputStream("D:\\a.txt");
		fos.write(97);
	}catch(IOException e){
	   e.printStackTrace();
	}finally {
		//finally语句里面的代码,一定会被执行.
		if(fos != null){
			try {
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

五.小结:

①创建字节输出流对象
  文件不存在,就创建。
  文件存在就清空。如果不想被清空则加true
②写数据
  可以写一个字节,写一个字节数组,写一个字节数组的一部分
  写一个回车换行:\r\n
③释放资源
  在捕获异常的finally中去关闭释放
  

六.更多精彩内容
http://www.gxcode.top/code
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java之书

会持续更新实用好的文章谢谢关注

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值