s-day09--使用字节输入流一次读取一个字节

This commit is contained in:
xx
2026-10-23 10:18:51 +08:00
parent a65374adac
commit 0fd06fdf6f
6 changed files with 61 additions and 7 deletions
@@ -0,0 +1,54 @@
package com.inmind.outputstream01;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/*
IO流的分类以及所有的顶层父类
IO流通过2种方式分类:
1.按流向分:
输入流
输出流
2.按传输的数据类型分:
字节流
字符流
在java中,io流分类:
字节输入流: InputStream
字节输出流:OutputStream
字符输入流:Reader
字符输出流:Writer
--------------------------------------------------
3.字节输出流的介绍以及相关的API
在java中有一个抽象父类OutputStream表示字节输出
常用子类:FileOutputStream
构造方法:
FileOutputStream(File file) 创建文件输出流以写入由指定的 File对象表示的文件。
FileOutputStream(String name) 创建文件输出流以指定的名称写入文件。
常用方法:
void write(int b) 将指定的字节写入此文件输出流。
void write(byte[] b) 将 b.length字节从指定的字节数组写入此文件输出流。
void write(byte[] b, int off, int len) 将 len字节从指定的字节数组开始,从偏移量 off开始写入此文件输出流。
void close() 释放任何系统资源。
*/
public class Demo01 {
public static void main(String[] args) throws IOException {
/*
1.创建一个字节输出流对象
a.如果指定文件不存在,自动创建
b.如果指定文件存在,它会默认覆盖
c.将fos对象与指定的硬盘中的文件进行了绑定,操作fos就是在操作文件
*/
OutputStream os = new FileOutputStream("a.txt");
//2.调用写功能
//void write(int b) 将指定的字节写入此文件输出流。
// os.write(97);
//'中'是不是1个字节?char->int(字节),此处出现了精度丢失,只写出一个字节的数据45
os.write('中');
//3.释放资源
os.close();
System.out.println("程序结束");
}
}
@@ -0,0 +1,8 @@
package com.inmind.outputstream01;
public class Demo02 {
public static void main(String[] args) {
System.out.println('中'+0);//20013,超出了1个字节的取值范围的
System.out.println('中'%256);//45
}
}
@@ -0,0 +1,37 @@
package com.inmind.outputstream01;
import java.util.Arrays;
/*
5.字节数组和字符串的相互转换
字节数组--->字符串:构造方法
String(byte[] bytes) 通过使用平台的默认字符集解码指定的字节数组来构造新的 String 。
String(byte[] bytes, int offset, int length) 通过使用平台的默认字符集解码指定的字节子阵列来构造新的 String
字符串--->字节数组:成员方法
byte[] getBytes() 使用平台的默认字符集将该 String编码为一系列字节,将结果存储到新的字节数组中。
注意:
1.UTF-8:字母和数字都只占1个字节,而一个中文占3个字节
2.GBK:字母和数字都只占1个字节,而一个中文占2个字节
*/
public class Demo03 {
public static void main(String[] args) {
String str = "abc";
//String->byte[]
byte[] bytes = str.getBytes();
System.out.println(Arrays.toString(bytes));// 97 98 99
//byte[]-->String
String s = new String(bytes);
System.out.println(s);
//中文转字节
String str1 = "中国";
byte[] bytes1 = str1.getBytes();
System.out.println(Arrays.toString(bytes1));
//字节转中文,帮我定义一个“国”字的字节数组,转为中文
byte[] bytes2 = {-27,-101,-67};
String str2 = new String(bytes2);
System.out.println(str2);
}
}
@@ -0,0 +1,28 @@
package com.inmind.outputstream01;
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();
}
}
@@ -0,0 +1,22 @@
package com.inmind.outputstream01;
import java.io.FileOutputStream;
/* 文件的换行:
Windows:\r\n
linux:\n
mac:\r
注意:在同一次流操作中,不停地调用write方法,后面的数据是不会覆盖前面的数据的,之前是new了新流才会覆盖
*/
public class Demo05 {
public static void main(String[] args) throws Exception{
//创建一个覆写的字节输出流
FileOutputStream fos = new FileOutputStream("a.txt");
//输出换行的我是谁我在哪我在干嘛
// fos.write("我是谁\r\n我在哪\r\n我在干嘛".getBytes());
fos.write(("我是谁"+System.lineSeparator()
+"我在哪"+System.lineSeparator()+"我在干嘛").getBytes());
fos.close();
}
}