From 9a12cf27a3214a1925512ab824a4eb10d9723094 Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Thu, 22 Oct 2026 16:07:08 +0800 Subject: [PATCH] =?UTF-8?q?s-day09--=E6=96=87=E4=BB=B6=E7=9A=84=E7=BB=AD?= =?UTF-8?q?=E5=86=99=E5=B9=B6=E5=86=99=E5=87=BA=E5=AD=97=E8=8A=82=E6=95=B0?= =?UTF-8?q?=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- s-day09/src/com/inmind/io01/Demo04.java | 29 +++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 s-day09/src/com/inmind/io01/Demo04.java diff --git a/s-day09/src/com/inmind/io01/Demo04.java b/s-day09/src/com/inmind/io01/Demo04.java new file mode 100644 index 0000000..89d8538 --- /dev/null +++ b/s-day09/src/com/inmind/io01/Demo04.java @@ -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(); + } +}