当前位置:网站首页>【无标题】
【无标题】
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();
}
}
边栏推荐
- Using GDB to quickly read the kernel code of PostgreSQL
- Vins Theory and Code detail 4 - Initialization
- 剑指 Offer 06. 从尾到头打印链表
- 一款完整的多用户微信公众平台开发源码,带文档免费分享
- Digital management medium + low code, jnpf opens a new engine for enterprise digital transformation
- 一文带你了解J.U.C的FutureTask、Fork/Join框架和BlockingQueue
- Click to unlock "keyword" of guanghetong 5g module
- "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
- Detailed explanation of binary search
- Summary of methods for point projection onto a plane
猜你喜欢

4、再遇Panuon.UI.Silver之窗体标题栏

Day10/11 递归 / 回溯

云图说|每个成功的业务系统都离不开APIG的保驾护航

Detailed explanation of binary search

One-way hash function

凸函数的Hessian矩阵与高斯牛顿下降法增量矩阵半正定性的理解

"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

This article introduces you to j.u.c's futuretask, fork/join framework and BlockingQueue

Yuntu says that every successful business system cannot be separated from apig

Golang beep package playback MP3 cannot get the total length streamer Len() is 0, but other formats can
随机推荐
影刀RPA学习和遇见excel部分问题解决方式
Methods commonly used in uniapp (part) - timestamp problem and rich text parsing image problem
Cmake actual combat record (I)
MySQL8安装详细步骤
[rust daily] 2022-04-19 performance evaluation of rust asynchronous framework
ORB_ Slam2 visual inertial tight coupling positioning technology route and code explanation 3 - tight coupling optimization model
Net core Tianma XingKong series - Interface Implementation for dependency injection and mutual conversion of database tables and C entity classes
Remote monitoring and data acquisition solution
22. Generate Parentheses
Huawei cloud SRE deterministic O & M introduction
rk3399_ 9.0 first level menu Network & Internet without setting
TensorFlow实战Google深度学习框架第二版学习总结-TensorFlow入门
Opentelemetry metrics release candidate
Even some people say that ArrayList is twice as large. Today, I will take you to tear up the ArrayList source code
面试题详情
Interview question details
Cube 技术解读 | Cube 渲染设计的前世今生
初学pytorch踩坑
广和通携手中国移动、惠普、联发科、英特尔合作打造5G全互联PC泛终端系列产品
点击解锁广和通5G模组“关键词”