进阶day09-文件的续写和换行

This commit is contained in:
2026-03-21 11:20:17 +08:00
parent 54ea3ca515
commit 5f3a7ef347
2 changed files with 34 additions and 1 deletions

View File

@@ -15,7 +15,7 @@ import java.util.Arrays;
1.UTF-8:字母和数字都只占1个字节,而一个中文占3个字节
2.GBK:字母和数字都只占1个字节,而一个中文占2个字节
*/
public class Demo4 {
public class Demo04 {
public static void main(String[] args) {
String str = "abc";
//字符串--->字节数组

View File

@@ -0,0 +1,33 @@
package com.inmind.io01;
import java.io.FileNotFoundException;
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开始写入此文件输出流。
----------------------------------------------------------------------------------
文件内容的换行
windows\r\n
linux: \n
mac: \r
*/
public class Demo05 {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("a.txt", true);
// fos.write("我是谁,我在哪,我在干嘛".getBytes());
// fos.write("我是谁我在哪我在干嘛".getBytes(),0,9);
// fos.write("我是谁\r\n我在哪\r\n我在干嘛".getBytes());
fos.write(("我是谁"+System.lineSeparator()+"我在哪我在干嘛").getBytes());
fos.close();
}
}