当前位置:网站首页>【无标题】
【无标题】
2022-06-10 15:27:00 【CaraYQ】
字节缓冲流
字节缓冲输入流
一、java.io.BufferedInputStream extends InputStream
- BufferedInputStream:字节缓冲输入流
- 继承自父类的成员方法:
(1)int read():从输入流中读取数据的下一个字节
(2)int read(byte[] b):从输入流中读取一定数量的字节 并将其存储在缓冲区数组b中
(3)void close():关闭此输入流并释放与该流关联的所有系统资源 - 构造方法:
(1)BufferedInputStream(InputStream in):创建一个BufferedInputStream并保存其参数 即输入流in以便将来使用
(2)BufferedInputStream(InputStream in,int size):创建具有指定缓冲区大小的BufferedInputStream并保存其参数 即输入流in以便将来使用 - 使用步骤:
(1)创建FileInputStream对象 构造方法中绑定要读取的数据源
(2)创建BufferedInputStream对象 构造方法中传递FileInputStream对象 提高FileInputStream对象效率
(3)使用BufferedInputStream对象中的方法read读取文件
(4)释放bufferedInputStream资源
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.BufferedInputStream;
public class BufferedInputStreamClass {
public static void main(String[] args) throws IOException{
// * 1.创建FileInputStream对象 构造方法中绑定要读取的数据源
FileInputStream fis = new FileInputStream("D:\\JAVA\\source\\Day21\\src\\a.txt");
// * 2.创建BufferedInputStream对象 构造方法中传递FileInputStream对象 提高FileInputStream对象效率
BufferedInputStream bis = new BufferedInputStream(fis);
// * 3.使用BufferedInputStream对象中的方法read 读取文件
// * int read():从输入流中读取数据的下一个字节
// int len = 0;//记录每次读取到的字节
// while ((len = bis.read()) != -1) {
// System.out.println(len);
// }
// * int read(byte[] b):从输入流中读取一定数量的字节 并将其存储在缓冲区数组b中
byte[] bytes = new byte[1024];//存储每次读取的数据
int len = 0;//记录每次读取到的有效字节个数
while((len = bis.read(bytes)) != -1) {
System.out.println(new String(bytes,0,len));
}
// * 4.释放资源
bis.close();
}
}
字节缓冲输出流
一、java.io.BufferedOutputStream extends OutputStream
BufferedOutputStream字节缓冲输出流- 继承来自父类的共性方法:
(1)public void close():关闭此输出流并释放与此流相关联的任何系统资源
(2)public void flush():刷新此输出流并强制任何缓冲的输出字节被写出
(3)public void write(byte[] b):将b.length字节从指定的字节数组写入此输出流
(4)public void write(byte[] b,int off,int len):从指定的字节数组写入len字节 从偏移量off开始输出到此输出流
(5)publid abstract void write(int b):将指定的字节输出流 - 构造方法:
(1)BufferedOutputStream(OutputStream out):创建一个新的缓冲输出流 以将数据写入指定的底层输出流
(2)BufferedOutputStream(OutputStream out,int size):创建一个新的缓冲输出流 以将具有指定缓冲区大小的数据写入指定的底层输出流
参数:
OutputStream out:字节输出流 我们可以传递FileOutputStream缓冲流会给FileOutputStream增加一个缓冲区 提高FileOutputStream的写入效率int size:指定缓冲流内部缓冲区的大小 不指定默认
- 使用步骤:
(1)创建FileOutputStream对象 构造方法中绑定要输出的目的地
(2)创建BufferedOutputStream对象 构造方法中传递FileOutputStream对象 提高FileOutputStream对象效率
(3)使用BufferedOutputStream对象中的方法write把数据写入到内部缓冲区中
(4)使用BufferedOutputStream对象中的方法flush把内部缓冲区中的数据 刷新到文件中
(5)释放BufferedOutputStream资源(会先调用flush方法刷新数据 第四步可以省略)
二、文件存储的原理:写数据时 会把十进制的整数转换为二进制整数
三、任意的文本编辑器 在打开文件时 都会查询编码表 把字节转换为字符表示 0-127查询ascii表 其他值则查询系统默认码表(中文系统查询GBK)
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.BufferedOutputStream;
public class BufferedOutputStreamClass {
public static void main(String[] args) throws IOException{
// * 1.创建FileOutputStream对象 构造方法中绑定要输出的目的地
FileOutputStream fos = new FileOutputStream("D:\\JAVA\\source\\Day21\\src\\a.txt");
// * 2.创建BufferedOutputStream对象 构造方法中传递FileOutputStream对象 提高FileOutputStream对象效率
BufferedOutputStream bos = new BufferedOutputStream(fos);
// * 3.使用BufferedOutputStream对象中的方法write 把数据写入到内部缓冲区中
bos.write("我把数据写入到内部缓冲区中".getBytes());
// * 5.释放资源(会先调用flush方法刷新数据 第四步可以省略)
bos.close();
}
}
/*
- java.io.BufferedReader extends Reader
- 继承自父类的共性成员方法:
- int read():读取单个字符并返回
- int read(char[] cbuf):一次读取多个字符 将字符读入数组
- void close():关闭该流并释放与之关联的所有资源
- 构造方法:
- BufferedReader(Reader in):创建一个使用默认大小输入缓冲区的缓冲字符输入流
- BufferedReader(Reader in,int sz):创建一个使用指定大小输入缓冲区的缓冲字符输入流
- 参数:Reader in字符输入流 我们可以传递FileReader 缓冲流会给FileReader增加一个缓冲区 提高FileReader读取效率
- 特有的成员方法:
- String readLine():读取一个文本行 行的终止符号:通过以下字符之一认为某行已终止 换行\n 回车\r 回车换行\r\n
- 返回值:包含该行内容的字符串 不包含任何行终止符 如果已到达流末尾 则返回null
- 使用步骤:
- 1.创建字符缓冲输入流对象 构造方法中传递字符输入流
- 2.使用字符缓冲输入流对象中的方法read/readLine读取文本
- 3.释放资源
import java.io.FileReader;
import java.io.IOException;
import java.io.BufferedReader;
public class BufferedReaderClass {
public static void main(String[] args) throws IOException{
//1.创建字符缓冲输入流对象 构造方法中传递字符输入流
BufferedReader br = new BufferedReader(new FileReader("D:\\\\JAVA\\\\source\\\\Day20\\\\src\\\\b.txt"));
//2.使用字符缓冲输入流对象中的方法read/readLine读取文本
// String lineString = br.readLine();
// System.out.println(lineString);
/*如果要读取多行 则需要将以上代码复制多次 因此 我们可以用循环来实现 * 假设我们需要将文档中的内容全部读完 我们并不知道到底有多少行 所以用while循环 */
String lineString;
while ((lineString = br.readLine())!=null) {
System.out.println(lineString);
}
//3.释放资源
br.close();
}
}
/*
- java.io.BufferedWriter extends Writer
- BufferedWriter:字符缓冲输入流
- 继承来自父类的共性成员方法:
- void write(int c):写入单个字符
- void write(char[] cbuf):写入字符数组
- abstract void write(char[] cbuf,int off,int len):写入字符数组的某一部分 off数组是我开始索引 len写的字符个数
- void write(String str):写入字符串
- void write(String str,int off,int len):写入字符串的某一部分 off字符串的开始索引 len写的字符个数
- void flush():刷新该流的缓冲
- void close():关闭此流 但要先刷新他
- 构造方法:
- BufferedWriter(Writer out):创建一个使用默认大小输出缓冲区的缓冲字符输出流
- BufferedWriter(Write out.int sz):创建一个使用给定大小输出缓冲区的新缓冲字符输出流
- 参数:
- Writer out:字符输出流 我们可以传递FileWriter 缓冲流会给FileWriter增加一个缓冲区 提高FileWriter的写入效率
- int sz:指定缓冲区的大小 不写默认大小
- 特有的成员方法:
- void newLine():写入一个行分隔符 会根据不同的操作系统 获取不同的行分隔符
- 换行:换行符号
- windows:\r\n
- linux:/n
- mac:/r
- 使用步骤:
- 1.创建字符缓冲输出流对象 构造方法中传递字符输出流
- 2.调用字符缓冲输出流中的方法write 把数据写入到内存缓冲区中
- 3.调用字符缓冲输出流中的方法flush 把内存缓冲区中的数据刷新到文件中
- 4.释放资源
*/
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterClass {
public static void main(String[] args) throws IOException {
// * 1.创建字符缓冲输出流对象 构造方法中传递字符输出流
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\JAVA\\source\\Day21\\src\\a.txt"));
// * 2.调用字符缓冲输出流中的方法write 把数据写入到内存缓冲区中
for (int i = 0; i < 10; i++) {
bw.write("传智播客");
bw.write("\r\n");
}
// * 3.调用字符缓冲输出流中的方法flush 把内存缓冲区中的数据刷新到文件中
bw.flush();
// * 4.释放资源
bw.close();
}
}
/*
- FileReader可以读取IDE默认编码格式(UTF-8)的文件 如果读取系统默认编码(中文系统GBK)会产生乱码
*/
import java.io.IOException;
import java.io.FileReader;
public class FileReaderClass {
public static void main(String[] args) throws IOException{
FileReader fReader = new FileReader("D:\\JAVA\\source\\Day21\\bin\\我是GBK格式的文档.txt");
int len = 0;
while ((len = fReader.read())!=-1) {
System.out.print((char)len);
}
fReader.close();
}
}
边栏推荐
- RSA a little bit of thought
- How does CRM help enterprises and salespeople?
- json.load(s)与json.dump(s)
- 港大、英伟达 | Factuality Enhanced Language Models for Open-Ended Text Generation(用于开放式文本生成的事实性增强语言模型)
- How to solve the problem that SVN cannot open the URL address
- Cap version 6.1 Release Notice
- 华为云SRE确定性运维介绍
- cmake实战记录(一)
- 顺应医改,积极布局——集采背景下的高值医用耗材发展洞察2022
- Docket command
猜你喜欢

ORB_SLAM2视觉惯性紧耦合定位技术路线与代码详解3——紧耦合优化模型

Vins theory and code explanation 0 -- theoretical basis in vernacular

How does the wireless communication module help the intelligent UAV build the "Internet of things in the air"?

uniapp中常用到的方法(部分) - 時間戳問題及富文本解析圖片問題

虚拟机ping不通的几种原因及解决办法

In what scenario can we not use the arrow function?

Guanghetong high computing power intelligent module injects intelligence into 5g c-v2x in the trillion market

这几个垂直类小众导航网站,你绝对不会想错过

opencv#4 手写体识别:自建训练集完美

企业如何提升文档管理水平
随机推荐
信息论与编码2 期末复习-BCH码
Yuntu says that every successful business system cannot be separated from apig
opencv神经网络库之SVM和ANN_MLP的使用
Create a space of local value together. In 2022, China successfully held the "one hundred cities tour · Ningbo Station" for commercial distribution
ORB_ Slam2 visual inertial tight coupling positioning technology route and code explanation 2 - IMU initialization
ORB_SLAM2视觉惯性紧耦合定位技术路线与代码详解3——紧耦合优化模型
opencv#4 手写体识别:自建训练集完美
港大、英伟达 | Factuality Enhanced Language Models for Open-Ended Text Generation(用于开放式文本生成的事实性增强语言模型)
Fast detection of short text repetition rate
Guanghetong high computing power intelligent module injects intelligence into 5g c-v2x in the trillion market
"Bloom Cup" 5g Application Award grand slam! Several joint projects of guanghetong won the first, second and third prizes in the general product theme competition
虚拟机ping不通的几种原因及解决办法
The power of insight
VINS理论与代码详解0——理论基础白话篇
uniapp中常用到的方法(部分) - 時間戳問題及富文本解析圖片問題
Applet network request promise
RSA a little bit of thought
Cmake actual combat record (I)
4、再遇Panuon.UI.Silver之窗体标题栏
[reward publicity] [content co creation] issue 16 may Xu sublimation, create a good time! You can also win a gift package of up to 500 yuan if you sign a contract with Huawei cloud Xiaobian!