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

This commit is contained in:
xx
2026-10-22 16:07:08 +08:00
parent 848d22b108
commit 9a12cf27a3
+29
View File
@@ -0,0 +1,29 @@
package com.inmind.io01;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
/*
6.文件的续写并写出字节数组
续写:默认会将原本文件中的数据覆盖,但是我们想继续拼接输入
续写就是使用重载的构造方法即可,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 Exception {
//1.创建一个可以续写的字节输出流
FileOutputStream fos = new FileOutputStream("a.txt", true);
//2.调用写方法,写出字节数组void write(byte[] b)
String str = "我是谁我在哪我在干嘛";
byte[] bytes = str.getBytes();
// void write(byte[] b, int off, int len),我想输出“我在哪我在干嘛”
// fos.write(bytes);
fos.write(bytes,9,21);
//3.资源释放
fos.close();
}
}