IO流

一、流的基本分类

1.操作数据单位:字节流、字符流

2.数据的流向:输入流、输出流

3.流的角色:节点流、处理流

 

二、流的体系结构

抽象基类                                  节点流(或文件流)                            缓冲流(处理流的一种)           转换流

InputStream                             FileInputStream                                   BufferedInputStream                           字节流

OutputStream                          FileOutputStream                                 BufferedOutputStream                                             字节流

Reader                                     FileReader                                           BufferedReader        InputStreamReader            字符流

Writer                                       FileWriter                                             BufferedWriter        OutputStreamReader          字符流

 

三、节点流

 

字符流适用于文本文件

复制代码
 1 import org.testng.annotations.Test;
 2 
 3 import java.io.File;
 4 import java.io.FileNotFoundException;
 5 import java.io.FileReader;
 6 import java.io.IOException;
 7 
 8 public class TestFileReader {
 9 
10     public static void main(String[] args) throws IOException {
11         //1.实例化File类对象,指明要操作的文件
12         File file = new File("hello.txt");
13         //2.提供具体的流
14         FileReader fr = new FileReader(file);
15         //3.数据的读入
16         int data = fr.read();
17         while(data != -1){
18             System.out.println((char)data);
19             data = fr.read();
20         }
21         //4.流的关闭操作
22         fr.close();
23     }
24 
25 }
复制代码
复制代码
 1 @Test
 2     public void testFileWriter() throws IOException {
 3         //1.实例化File类对象,指明要操作的文件
 4         File file = new File("C:\\Users\\12582\\IdeaProjects\\untitled\\src\\hello1.txt");
 5         //2.提供具体的流
 6         FileWriter fw = new FileWriter(file);
 7         //3.数据的写入
 8         fw.write("I have a dream!\n");
 9         fw.write("I have a dream!");
10         //4.流的关闭操作
11         fw.close();
12     }
复制代码

 

注意:字符流( FileReader 、  FileWriter )不能处理图片,需要使用字节流(   FileInputStream  、FileOutputStream  )

 

字节流适用于非文本文件(图片、视频。。。)

 

四、缓冲流(处理流的一种)

复制代码
 1 import org.testng.annotations.Test;
 2 
 3 import java.io.*;
 4 
 5 public class BufferedStreamTest {
 6 
 7     @Test
 8     public void BufferedStreamTest() throws IOException {
 9         //1.造文件
10         File srcFile = new File("C:\\Users\\12582\\IdeaProjects\\untitled\\src\\外国友人.jpg");
11         File destFile = new File("C:\\Users\\12582\\IdeaProjects\\untitled\\src\\外国友人2.jpg");
12         //2.造流
13         //2.1造节点流
14         FileInputStream fis = new FileInputStream((srcFile));
15         FileOutputStream fos = new FileOutputStream(destFile);
16         //2.2造缓冲流
17         BufferedInputStream bis = new BufferedInputStream(fis);
18         BufferedOutputStream bos = new BufferedOutputStream(fos);
19         //3.复制的细节:读取、写入
20         byte[] buffer = new byte[10];
21         int len;
22         while((len = bis.read(buffer)) != -1){
23             bos.write(buffer, 0, len);
24         }
25 
26         //4.资源关闭
27         //要求先关闭外层的流(缓冲流),再关闭内层的流(关闭外层流的时候,内层会自动关闭,所以可以省略不写)
28         bos.close();
29         bis.close();
30 
31     }
32 }
复制代码

 

 

五、转换流(处理流的一种)

 

 

 

 

 

 

六、字符集

 

 

 

七、标准的输入输出流

System.in

System.out

 

八、打印流

PrintStream

PrintWriter

 

九、数据流

DataInputStream

DataOutputStream

用于读取或写出基本数据类型的变量或字符串

posted @   草莓小甜心  阅读(35)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 我与微信审核的“相爱相杀”看个人小程序副业
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~
点击右上角即可分享
微信分享提示