1.字节输出流写出数据的三种方式 2.换行与续写

package com;
import java.io.FileOutputStream;
import java.io.IOException;
public class demo {
    public static void main(String[] args) throws IOException {
        //路径必须写全C:\\Users\\彭新晨\\IdeaProjects\\untitled6\\a.txt
        //创建对象
        FileOutputStream fos=new FileOutputStream("C:\\Users\\彭新晨\\IdeaProjects\\untitled6\\a.txt");

        /*//第一种 创建一个字节数数据
        fos.write(97);


        //第二种 创建一个字节数组数据
        byte byes[]={97,98,99,100,101};
        fos.write(byes);
         */

        //第三种 创建数组  起始和起始后一共几个元素     一个字节数组的部分数据
        byte byes[]={97,98,99,100,101};
        //off是起始  按照索引来的是98   len是后推多少个元素
        fos.write(byes,1,2);

        //释放资源
        fos.close();
    }
}

 

package com;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class demo {
    public static void main(String[] args) throws IOException {
        //创建   续写开关  在创建对象参数第二个 默认是false 改为true就能续写 不清空了
        FileOutputStream fos=new FileOutputStream("C:\\Users\\彭新晨\\IdeaProjects\\untitled6\\a.txt",true);
        //写出数据
        String str="pgzs";
        byte[] bytes = str.getBytes();
        fos.write(bytes);
        //换行操作
        String str1="\r\n";
        byte[] bytes1 = str1.getBytes();
        fos.write(bytes1);
        //666
        String str2="666";
        byte[] bytes2 = str2.getBytes();
        fos.write(bytes2);

        //释放资源
        fos.close();
    }
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值