s-day09-文件的续写并写出字节数组

This commit is contained in:
2026-08-09 16:53:05 +08:00
parent de939c9b7c
commit aeca9c4177
@@ -0,0 +1,24 @@
package com.inmind.io_out01;
import java.io.FileOutputStream;
import java.io.IOException;
/*
文件的续写并写出字节数组
续写:默认会将原本文件中的数据覆盖,但是我们想继续拼接输入
续写就是使用重载的构造方法即可,true:续写 false:覆盖
FileOutputStream(String name, boolean append) 创建文件输出流以指定的名称写入文件。
写出字节数组
void write(byte[] b) 将 b.length字节从指定的字节数组写入此文件输出流。
void write(byte[] b, int off, int len) 将 len字节从指定的字节数组开始,从偏移量 off开始写入此文件输出流。
*/
public class Demo04 {
public static void main(String[] args) throws IOException {
//往a.txt中续写:“我是谁,我在哪,我在干嘛”
FileOutputStream fos = new FileOutputStream("a.txt",true);
String str = ",我在哪,我在干嘛";
byte[] bytes = str.getBytes();
fos.write(bytes);
fos.close();
}
}