Java I/O

  

 

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
package ersatz;
 
import org.junit.jupiter.api.Test;
 
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
 
public class Ersatz {
  public static void main(String[] args) {
 
  }
 
  @Test
  public void m1() {
    int data = 0;
    FileInputStream fileInputStream = null;
    try {
      fileInputStream = new FileInputStream("d:/n2");
      while ((data = fileInputStream.read()) != -1) {
        System.out.println((char) data + " " + data);
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        assert fileInputStream != null;
        fileInputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
 
  @Test
  public void m2() {
    byte[] buffer = new byte[2];
    int len = 0;
    FileInputStream fileInputStream = null;
    try {
      fileInputStream = new FileInputStream("d:/n2");
      while ((len = fileInputStream.read(buffer)) != -1) {
        System.out.println(new String(buffer, 0, len));
        for (byte b : buffer) {
          System.out.println(b);
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        assert fileInputStream != null;
        fileInputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
 
  @Test
  public void m3() throws IOException {
    FileOutputStream fileOutputStream = null;
    try {
      fileOutputStream = new FileOutputStream("d:/n1", true);
//      fileOutputStream.write(97788);
      String s = "uiopvbn发生付款啦";
      fileOutputStream.write(s.getBytes(StandardCharsets.UTF_8), 7, 3);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } finally {
      try {
        assert fileOutputStream != null;
        fileOutputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
 
  @Test
  public void m4() {
    String src = "d:/a.png";
    String dst = "d:/b.png";
    FileInputStream fileInputStream = null;
    FileOutputStream fileOutputStream = null;
    try {
      fileInputStream = new FileInputStream(src);
      fileOutputStream = new FileOutputStream(dst);
      byte[] buffer = new byte[1024];
      int length = 0;
      while ((length = fileInputStream.read(buffer)) != -1) {
        fileOutputStream.write(buffer, 0, length);
      }
      System.out.println("ok");
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (fileInputStream != null) fileOutputStream.close();
        if (fileOutputStream != null) fileOutputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

  

FileReader

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
package ersatz;
 
import org.junit.jupiter.api.Test;
 
import java.io.FileReader;
import java.io.IOException;
 
public class Ersatz {
  public static void main(String[] args) {
 
  }
 
  @Test
  public void fileReader1() {
    FileReader fileReader=null;
    int data;
    try {
      fileReader = new FileReader("d:/courgette.log");
      while ((data = fileReader.read()) != -1) {
        System.out.print((char) data);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }finally{
      try {
        if (fileReader != null) {
          fileReader.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
 
  @Test
  public void fileReader2() {
    FileReader fileReader=null;
    int length=0;
    char[] buffer = new char[8];
    try {
      fileReader = new FileReader("d:/courgette.log");
      while ((length = fileReader.read(buffer)) != -1) {
        System.out.print(new String(buffer, 0, length));
      }
    } catch (IOException e) {
      e.printStackTrace();
    }finally{
      try {
        if (fileReader != null) fileReader.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

  

FileWriter

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
package ersatz;
 
import java.io.FileWriter;
import java.io.IOException;
 
public class Ersatz {
  public static void main(String[] args) {
    FileWriter fileWriter=null;
    char[] chars={'a', '发', '法', 'g'};
 
    try {
      fileWriter = new FileWriter("d:/n3",false);
      fileWriter.write(98);
      fileWriter.write(chars);
      fileWriter.write("范德萨浪费".toCharArray(),0,3);
      fileWriter.write("微软微软离开",0,2);
    } catch (IOException e) {
      e.printStackTrace();
    }finally{
      try {
        assert fileWriter != null;
        fileWriter.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

  

 

BufferedWriter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class BufferedWriter_ {
    public static void main(String[] args) throws IOException {
        String filePath = "e:\\ok.txt";
        //创建BufferedWriter
        //说明:
        //1. new FileWriter(filePath, true) 表示以追加的方式写入
        //2. new FileWriter(filePath) , 表示以覆盖的方式写入
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath));
        bufferedWriter.write("hello, 飞洒发生!");
        bufferedWriter.newLine();//插入一个和系统相关的换行
        bufferedWriter.write("hello2, 飞洒发生!");
        bufferedWriter.newLine();
        bufferedWriter.write("hello3, 飞洒发生!");
        bufferedWriter.newLine();
 
        //说明:关闭外层流即可 , 传入的 new FileWriter(filePath) ,会在底层关闭
        bufferedWriter.close();
 
    }
}

  

BufferedReader

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
public class BufferedReader_ {
    public static void main(String[] args) throws Exception {
 
        String filePath = "e:\\a.java";
        //创建bufferedReader
        BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
        //读取
        String line; //按行读取, 效率高
        //说明
        //1. bufferedReader.readLine() 是按行读取文件
        //2. 当返回null 时,表示文件读取完毕
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
 
        //关闭流, 这里注意,只需要关闭 BufferedReader ,因为底层会自动的去关闭 节点流
        //FileReader。
        /*
            public void close() throws IOException {
                synchronized (lock) {
                    if (in == null)
                        return;
                    try {
                        in.close();//in 就是我们传入的 new FileReader(filePath), 关闭了.
                    } finally {
                        in = null;
                        cb = null;
                    }
                }
            }
 
         */
        bufferedReader.close();
 
    }
}

  

Buffered copy\

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
package ersatz;
 
import java.io.*;
 
public class Ersatz {
  public static void main(String[] args) throws Exception {
    String src = "d:/a.png";
    String dst = "d:/c.png";
    BufferedInputStream bufferedInputStream=null;
    BufferedOutputStream bufferedOutputStream=null;
    int data;
    try {
      bufferedInputStream = new BufferedInputStream(new FileInputStream(src), 1024);
      bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(dst), 1024);
      while ((data = bufferedInputStream.read()) != -1) {
        bufferedOutputStream.write(data);
      }
      System.out.println("done");
    } catch (IOException e) {
      e.printStackTrace();
    }finally{
      try {
        if (bufferedInputStream != null) bufferedInputStream.close();
        if (bufferedOutputStream != null) bufferedOutputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

  

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
public class BufferedCopy_ {
 
    public static void main(String[] args) {
 
        // 1. BufferedReader 和 BufferedWriter 是安装字符操作
        // 2. 不要去操作 二进制文件[声音,视频,doc, pdf ], 可能造成文件损坏
        // BufferedInputStream
        // BufferedOutputStream
        String srcFilePath = "e:\\a.java";
        String destFilePath = "e:\\a2.java";
 
        BufferedReader br = null;
        BufferedWriter bw = null;
        String line;
        try {
            br = new BufferedReader(new FileReader(srcFilePath));
            bw = new BufferedWriter(new FileWriter(destFilePath));
 
            // 说明: readLine 读取一行内容,但是没有换行
            while ((line = br.readLine()) != null) {
                // 每读取一行,就写入
                bw.write(line);
                // 插入一个换行
                bw.newLine();
            }
            System.out.println("拷贝完毕...");
 
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭流
            try {
                if (br != null) {
                    br.close();
                }
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
 
    }
}

  

 

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
package com.hspedu.outputstream_;
 
import java.io.*;
 
/**
 * @version 1.0
 * 演示使用BufferedOutputStream 和 BufferedInputStream使用
 * 使用他们,可以完成二进制文件拷贝.
 * 思考:字节流可以操作二进制文件,可以操作文本文件吗?当然可以
 */
public class BufferedCopy02 {
    public static void main(String[] args) {
 
//        String srcFilePath = "e:\\Koala.jpg";
//        String destFilePath = "e:\\hsp.jpg";
//        String srcFilePath = "e:\\0245_Java_引出this.avi";
//        String destFilePath = "e:\\hsp.avi";
        String srcFilePath = "e:\\a.java";
        String destFilePath = "e:\\a3.java";
 
        //创建BufferedOutputStream对象BufferedInputStream对象
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
 
        try {
            //因为 FileInputStream  是 InputStream 子类
            bis = new BufferedInputStream(new FileInputStream(srcFilePath));
            bos = new BufferedOutputStream(new FileOutputStream(destFilePath));
 
            //循环的读取文件,并写入到 destFilePath
            byte[] buff = new byte[1024];
            int readLen = 0;
            //当返回 -1 时,就表示文件读取完毕
            while ((readLen = bis.read(buff)) != -1) {
                bos.write(buff, 0, readLen);
            }
 
            System.out.println("文件拷贝完毕~~~");
 
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
 
            //关闭流 , 关闭外层的处理流即可,底层会去关闭节点流
            try {
                if(bis != null) {
                    bis.close();
                }
                if(bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
 
        }
 
 
    }
}

 

  

 

posted @   ascertain  阅读(57)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2020-08-05 OCR识别服务开发手册
2020-08-05 Jenkins常用配置
点击右上角即可分享
微信分享提示