IO流

节点流和处理流:#

节点流类型

 

案例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package cn.test;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
 
import org.junit.Test;
 
public class Demo4  {
     
    @Test
    //测试字节输入流
    public void FileInputStreamTest() throws Exception{
        //创建字节输入流
        FileInputStream fs = new FileInputStream("./src/cn/test/Demo4.java");
        //创建一个1024的数节字组,每次从流中最多截取1024字节
        byte[] bytes = new byte[1024];
        //用于保存实际读取的字节数
        int hasRead = 0;
        //使用循环重复的读取流中的字节,直到hasRead=-1
        while ((hasRead = fs.read(bytes))  > 0){
            System.out.println(new String(bytes,0,hasRead));
        }
        fs.close();
    }
     
    @Test
    //测试字符输入流
    public void FileReaderTest() throws IOException{
        //创建字符流
        try {
            FileReader fr = new FileReader("./src/cn/test/Demo4.java");
            //设置每次从文件中读取的文件字符数
            char[] cbuf = new char[256];
            //用于保存实际读取得字符数
            int hasRead = 0;
            while ((hasRead=fr.read(cbuf))>0){
                //将字符串数组转换成字符串输入
                System.out.println(new String(cbuf,0,hasRead));
            }
            fr.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
         
    }
     
    @Test
    //测试字节输出流,读取A文件的流到B文件中
    public void fileOutputStreamTest() throws IOException{
         
        try {
            //创建字节输入流
            FileInputStream fis = new FileInputStream("./src/cn/test/Demo4.java");
            //创建字节输出流,将Demo4.java的内容写到newfile.txt中
            FileOutputStream fos = new FileOutputStream("D:/temp/newfile.txt");
            //创建取字节流的容器字节数组
            byte[] bytes = new byte[1024];
            //用于保存实际读取的字节数
            int hasRead = 0;
            //循环从输入流中读取数据
            while ((hasRead = fis.read(bytes))>0){
                //没读取一次就写入输出流中
                fos.write(bytes, 0, hasRead);
            }
            fis.close();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
     
    @Test
    //测试字符输出流
    public void fileWriterTest(){
        try {
            FileWriter fw = new FileWriter("D://temp//test.txt");
            //在test.txt中写入内容
            fw.write("hello world!!!\r\n");
            fw.append("China");
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
     
    @Test
    //测试PrintStream字节输出处理流
    public void printStreamTest(){
        try {
            //创建一个输出流
            FileOutputStream fos = new FileOutputStream("D:/temp/test.txt");
            PrintStream ps = new PrintStream(fos);
            //使用PrintStream执行输出,将"普通字符串"写入到test.txt中
            ps.println("普通字符串");
            ps.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
     
    @Test
    //测试转换流和缓冲流
    public void inputStreamReaderTest() throws IOException{
        try {
            //创建字节输入流
            FileInputStream fis = new FileInputStream("D:/temp/test.txt");
            //将字节输入流转换成字符输入流
            InputStreamReader reader = new InputStreamReader(fis);
            //将普通的Reader包装成BufferedReader
            BufferedReader br = new BufferedReader(reader);
            String line = null;
            //采取循环逐行读取
            while ((line = br.readLine()) != null){
                if (line.equals("exit")){
                    System.exit(1);
                }
                System.out.println(line);
            }
            br.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

  

posted @   头痛不头痛  阅读(181)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
点击右上角即可分享
微信分享提示
主题色彩