• 【Java】IO流原理及流的分类(FileInputStream和FileOutputStream方法详解)



    活动地址:CSDN21天学习挑战赛

    1.Java IO流原理

    1.I/O是Input/Output的缩写,用于数据传输,读写文件网络通讯。
    2.Java程序中,对于数据的输入/输出以流(stream)的方式进行。

    2.InputStream字节输入流

    InputStream抽象类是所有类字节输入流的超类

    InputStream常用子类

    1.FileInputStream:文件输入流
    2.BufferedInputStream:缓冲字节输入流
    3.ObjectInputStream:对象字节输入流

    1.FileInputStream文件输入流

    //创建 FileInputStream 对象,用于读取 文件
    fileInputStream = new FileInputStream(filePath);
    //从该输入流读取一个字节的数据。 如果没有输入可用,此方法将阻止。
    //如果返回-1 , 表示读取完毕

    package com.inputstream;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    /**
     * @version 1.0
     * @auther Demo龙
     * 演示FileInputStream的使用(字节输入流 文件--> 程序)
     *  */
    
    public class FileInputStresam {
        public static void main(String[] args) {
            Test01 test01 = new Test01();
            test01.readFile01();
        }
    }
    class Test01{
        public void readFile01(){
            String filePath = "e:\\hello.txt";
            int readData = 0;
            FileInputStream fileInputStream = null;
            try {
                //创建 FileInputStream 对象,用于读取 文件
                fileInputStream = new FileInputStream(filePath);
                //从该输入流读取一个字节的数据。 如果没有输入可用,此方法将阻止。
                //如果返回-1 , 表示读取完毕
                while ((readData = fileInputStream.read()) != -1) {
                    System.out.print((char)readData);//转成char显示
                }
    
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                //关闭文件流,释放资源.
                try {
                    fileInputStream.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

    //最后要关闭文件流,释放资源.

    3.FileOutputStream文件输出流

    OutputStream方法

    //1. new FileOutputStream(filePath) 创建方式,当写入内容是,会覆盖原来的内容

            //2. new FileOutputStream(filePath, true) 创建方式,当写入内容是,是追加到文件后面
    
    • 1
          3.  //写入一个字节6        
           //fileOutputStream.write('H');//
    
    • 1
    • 2
           4. //写入字符串
            String str = "hello,world,Demo!";
            //str.getBytes() 可以把 字符串-> 字节数组
            //fileOutputStream.write(str.getBytes());
    
    • 1
    • 2
    • 3
    • 4

    5.write(byte[] b, int off, int len) 将 len字节从位于偏移量 off的指定字节数组写入此文件输出流

    package com.outputstream;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    /**
     * @version 1.0
     * @auther Demo龙
     */
    public class Fileoutputstream {
        public static void main(String[] args) {
            Test test = new Test();
            test.writefile();
        }
    }
    class Test{
        public void writefile(){
            //创建 FileOutputStream对象
            String filePath = "e:\\a.txt";
            FileOutputStream fileOutputStream = null;
            try {
                //得到 FileOutputStream对象 对象
                //老师说明
                //1. new FileOutputStream(filePath) 创建方式,当写入内容是,会覆盖原来的内容
                //2. new FileOutputStream(filePath, true) 创建方式,当写入内容是,是追加到文件后面
                fileOutputStream = new FileOutputStream(filePath, true);
                //写入一个字节6
                //fileOutputStream.write('H');//
                //写入字符串
                String str = "hello,world,Demo!";
                //str.getBytes() 可以把 字符串-> 字节数组
                //fileOutputStream.write(str.getBytes());
                /*
                write(byte[] b, int off, int len) 将 len字节从位于偏移量 off的指定字节数组写入此文件输出流
                 */
                fileOutputStream.write(str.getBytes(), 0, str.length());
    
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    fileOutputStream.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

    演示结果
    在这里插入图片描述

    3.文件拷贝

    //完成 文件拷贝,将 e:\demo.jpg 拷贝 e:\demo2.jpg
    //思路分析
    //1. 创建文件的输入流 , 将文件读入到程序
    //2. 创建文件的输出流, 将读取到的文件数据,写入到指定的文件.

    try {
    //文件输入流
    fileInputStream = new FileInputStream(srcFilePath);
    //文件输出流
    fileOutputStream = new FileOutputStream(destFilePath);
    //定义一个字节数组,提高读取效果
    byte[] buf = new byte[1024];
    int readLen = 0;
    while ((readLen = fileInputStream.read(buf)) != -1) {
    //读取到后,就写入到文件 通过 fileOutputStream
    //即,是一边读,一边写
    fileOutputStream.write(buf, 0, readLen);//一定要使用这个方法

    package com.outputstream;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    /**
     * @version 1.0
     * @auther Demo龙
     */
    public class FileCopy {
        public static void main(String[] args) {
            //完成 文件拷贝,将 e:\\demo.jpg 拷贝 e:\\demo2.jpg
            //思路分析
            //1. 创建文件的输入流 , 将文件读入到程序
            //2. 创建文件的输出流, 将读取到的文件数据,写入到指定的文件.
            String srcFilePath = "e:\\demo.jpg";
            String destFilePath = "e:\\demo2.jpg";
            FileInputStream fileInputStream = null;
            FileOutputStream fileOutputStream = null;
    
            try {
                //文件输入流
                fileInputStream = new FileInputStream(srcFilePath);
                //文件输出流
                fileOutputStream = new FileOutputStream(destFilePath);
                //定义一个字节数组,提高读取效果
                byte[] buf = new byte[1024];
                int readLen = 0;
                while ((readLen = fileInputStream.read(buf)) != -1) {
                    //读取到后,就写入到文件 通过 fileOutputStream
                    //即,是一边读,一边写
                    fileOutputStream.write(buf, 0, readLen);//一定要使用这个方法
    
                }
                System.out.println("拷贝ok~");
    
    
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    //关闭输入流和输出流,释放资源
                    if (fileInputStream != null) {
                        fileInputStream.close();
                    }
                    if (fileOutputStream != null) {
                        fileOutputStream.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

    演示结果
    在这里插入图片描述

  • 相关阅读:
    GEE引擎架设好之后进游戏时白屏的解决方法——gee引擎白屏修复
    算法打卡_第一天:顺序查找+直接插入排序
    中山市 香山杯2023 Misc pintu
    Vue常见指令补充(附加案例)
    系统学习SpringFramework:SpringBean的注入方式
    企业电子招投标采购系统——功能模块&功能描述+数字化采购管理 采购招投标
    系统升级丨酷雷曼4大功能优化,轻松完成VR全景制作
    【网络】五种IO模型以及select编程详讲
    Vue路由及Node.js环境搭建
    idea启动报错javac <options> <source files>
  • 原文地址:https://blog.csdn.net/qq_59708493/article/details/126097966