| 字符流:处理字符相关,如处理⽂本数据(如txt⽂件), Reader/Writer |
| 字节流: 处理字节相关,如声⾳或者图⽚等⼆进制,InputStream/OutputStream |
| 字节流以字节(8bit)为单位,字符流以字符为单位,根据码表映射字符,⼀次可能读多个字节 |
| 字节流可以处理⼏乎所有⽂件,字符流只能处理字符类型的数据 |
| 字符流 Reader/Writer |
| 字节流 InputStream/OutputStream |

- InputStream是输⼊字节流的⽗类,它是⼀个抽象类(⼀般⽤他的⼦类)
| int read() |
| 从输⼊流中读取单个字节,返回0到255范围内的int字节值,字节数据可直接转换为int类 型, 如果已经到达流末尾⽽没有可⽤的字节,则返回-1 |
| int read(byte[] buf) |
| 从输⼊流中读取⼀定数量的字节,并将其存储在缓冲区数组buf中, 返回实际读取的字节数,如果已经到达流末尾⽽没有可⽤的字节,则返回-1 |
| long skip(long n) |
| 从输⼊流中跳过并丢弃 n 个字节的数据。 |
| int available() |
| 返回这个流中有多少个字节数,可以把buf数组⻓度定为这个 |
| void close() throws IOException |
| 关闭输⼊流并释放与该流关联的系统资源 |
| 抽象类InputStream⽤来具体实现类的创建对象, ⽂件字节输⼊流, 对⽂件数据以字节的形式进⾏读取操作 |
| |
| |
| public FileInputStream(String name) throws FileNotFoundException |
| |
| |
| public FileInputStream(File file) throws FileNotFoundException |
| public class Main { |
| |
| public static void main(String[] args)throws IOException { |
| String dir = "C:\\work"; |
| String name = "test.txt"; |
| File file = new File(dir,name); |
| InputStream inputStream = new FileInputStream(file); |
| testRead(inputStream); |
| testSkip(inputStream); |
| testReadByteArr(inputStream); |
| } |
| |
| |
| |
| |
| |
| |
| public static void testReadByteArr(InputStream inputStream)throws IOException{ |
| |
| |
| |
| |
| byte[] buf = new byte[inputStream.available()]; |
| int length; |
| |
| |
| while ((length = inputStream.read(buf))!=-1){ |
| |
| |
| System.out.print(new String(buf,0,length,"UTF-8")); |
| } |
| |
| inputStream.close(); |
| } |
| |
| |
| |
| |
| |
| |
| public static void testRead(InputStream inputStream)throws IOException{ |
| |
| int read = inputStream.read(); |
| System.out.println(read); |
| System.out.println((char)read); |
| |
| inputStream.close(); |
| } |
| |
| |
| |
| |
| |
| |
| public static void testSkip(InputStream inputStream)throws IOException{ |
| |
| long skipSize = inputStream.skip(2); |
| System.out.println(skipSize); |
| int read = inputStream.read(); |
| System.out.println(read); |
| System.out.println((char)read); |
| |
| inputStream.close(); |
| } |
| |
| } |
- OutputStream是输出字节流的⽗类,它是⼀个抽象类
| void write(int b) |
| 将指定的字节写⼊输出流 |
| void write(byte[] b)throws IOException |
| 将b.length个字节的byte数组写⼊当前输出流 |
| void flush() throws IOException |
| write是写到缓冲区中,可以认为是内存中,当缓冲区满时系统会⾃动将缓冲区的内容写⼊⽂件,但是⼀般还有⼀部分有可能会留在内存这个缓冲区中, 所以需要调⽤flush空缓冲区数据。 |
| void close() throws IOException |
| 关闭输⼊流并释放与该流关联的系统资源 |
- 抽象类OutputStream⽤来具体实现类的创建对象, ⽂件字节输出流, 对⽂件数据以字节的形式进⾏输出的操作
| |
| public FileOutputStream(String name) |
| |
| public FileOutputStream(File file) |
| |
| public FileOutputStream(File file, boolean append) |
| public class Main { |
| |
| public static void main(String[] args)throws IOException { |
| String dir = "C:\\work"; |
| String name = "test.txt"; |
| String target = "b.txt"; |
| File file = new File(dir,name); |
| InputStream inputStream = new FileInputStream(file); |
| |
| |
| |
| |
| OutputStream outputStream = new FileOutputStream(dir+File.separator+target,true); |
| |
| testOut(inputStream,outputStream); |
| testOutBuf(inputStream,outputStream); |
| } |
| |
| |
| |
| |
| |
| |
| |
| public static void testOutBuf(InputStream inputStream,OutputStream outputStream)throws IOException { |
| byte [] buf = new byte[1024]; |
| int length ; |
| while (( length = inputStream.read(buf))!=-1){ |
| outputStream.write(buf,0,length); |
| } |
| |
| inputStream.close(); |
| outputStream.close(); |
| } |
| |
| |
| public static void testOut(InputStream inputStream,OutputStream outputStream)throws IOException{ |
| int value = 0; |
| while (value!=-1){ |
| value = inputStream.read(); |
| outputStream.write(value); |
| } |
| |
| inputStream.close(); |
| outputStream.close(); |
| } |
| |
| } |
| 它是内存空间的⼀部分,在内存空间中预留了⼀定的存储空间,这些存储空间⽤来缓冲输⼊或输出的数据,这部分空间就叫做缓冲区,缓冲区是具有⼀定⼤⼩的 |
| BufferInputStream |
| BufferOutputStream |
| |
- BufferInputStream 缓冲字节输⼊流
| # BufferedInputStream 通过预先读⼊⼀整段原始输⼊流数据⾄缓冲区中,⽽外界对BufferedInputStream的读取操作实际上是在缓冲区上进⾏,如果读取的数据超过了缓冲区的范围,那么BufferedInputStream负责重新从原始输⼊流中载⼊下⼀截数据填充缓冲区,然后外界继续通过缓冲区进⾏数据读取 |
| # 好处:避免了⼤量的磁盘IO,原始的InputStream类实现的read是即时读取的,每⼀次读取都会是⼀次磁盘IO操作(哪怕只读取了1个字节的数据),如果数据量巨⼤,这样的磁盘消耗⾮常可怕 |
| # 缓冲区的实现: 读取可以读取缓冲区中的内容,当读取超过缓冲区的内容后再进⾏⼀次磁盘IO,载⼊⼀段数据填充缓冲,下⼀次读取⼀般情况就直接可以从缓冲区读取,减少了磁盘IO |
| # 默认缓冲区⼤⼩是8k, int DEFAULT_BUFFER_SIZE = 8192 |
| |
| |
| public BufferedInputStream(InputStream in); |
| |
| public BufferedInputStream(InputStream in,int size); |
| /从输⼊流中读取⼀个字节 |
| public int read(); |
| |
| |
| public int read(byte[] buf,int off,int len); |
| |
| |
| void close(); |
- BufferOutputStream 缓冲字节输出流
| |
| public BufferedOutputStream(OutputStream out); |
| |
| public BufferedOutputStream(OutputStream out,int size); |
| |
| # 常⽤的三个⽅法 |
| |
| public void write(int b); |
| |
| public void write(byte[] buf,int off,int len); |
| |
| public void flush(); |
| |
| void close(); |
| public class BufferMain { |
| |
| public static void main(String [] args){ |
| try{ |
| FileInputStream fis = new FileInputStream("C:\\Users\\79466\\Desktop\\test\\a.txt"); |
| BufferedInputStream bis = new BufferedInputStream(fis); |
| FileOutputStream fos = new FileOutputStream("C:\\Users\\79466\\Desktop\\test\\copy.txt"); |
| BufferedOutputStream bos = new BufferedOutputStream(fos); |
| int size; |
| byte [] buf = new byte[1024]; |
| while ( (size =bis.read(buf))!=-1){ |
| bos.write(buf,0,size); |
| } |
| |
| |
| bis.close(); |
| bos.close(); |
| }catch (Exception e){ |
| } |
| } |
| |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术