文件字节输出流

FileOutputStream

文件字节输出流

//textfile不存在时会自动新建文件,已存在时会覆盖原有
fos = new FileOutputStream("testfile");
//文件已存在时,会在原文件后面追加,不会覆盖掉原文件
fos = new FileOutputStream("testfile",true);
//将byte数组全部写出
byte[]bytes = {
    
    97,98,99,100,101,102};//abcdef
fos.write(bytes);
//再写入ab
fos.write(bytes,0,2);//ab
//写完之后一定要刷新
fos.flush();

注意点:

  • 创建对象时,两种方式。追加和覆盖

    FileOutputStream(String name,booblean append)

    new FileOutputStream(“testfile”,true)追加型写入

  • 写入的数字会自动通过askII码转为字母 97-a 98-b

  • 写完之后一定要 fos.flush()刷新

  • 写入String类型

    String string = "我正在学习文件字符输出流";
                //把字符串转变成数组getBytes()
                byte[]bytes2 = string.getBytes();
                fos.write(bytes2);
    

猜你喜欢

转载自blog.csdn.net/weixin_43903813/article/details/112727856
今日推荐