(Java笔记)IO流的六类16种流方式

(Java笔记)IO流的六类16种流方式,第1张

目录

一、文件流 

1.字节流

2.字符流

 二、转换流(字节流转字符流)

1.输入

2.输出

三、缓冲流

1.字节流

2.字符流+转换流

 四、数据流

1.DataInputStream

2.DataOutputStream

 五、标准输出流

1.DataInputStream

2.DataOutputStream

六、对象流

1.ObjectInputStream

2.ObjectOutputStream


学习IO流自建项目Github源码地址:https://github.com/GuiZhouAndroid/JavaIODemo


一、文件流 

1.字节流

(1)文件字节输入流:FileInputStream(读取硬盘,写入内存)

(2)文件字节输出流:FileOutputStream(读取内存、写入硬盘)

2.字符流

(3)文件字符输入流:FileReader(读取硬盘,写入内存)

(4)文件字符输出流:FileWriter(读取内存、写入硬盘)

        文件字节流案例:使用FileInputStream+FileOutputStream复制粘贴文件,如下:

package com.dhrj.zs.file;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * created by on 2022/4/26
 * 描述:文件拷贝:使用FileInputStream + FileOutputStream
 * 一边读一边写,字节流方式可拷贝任意类型文件
 *
 * @author ZSAndroid
 * @create 2022-04-26-16:50
 */
public class FileStreamCopyTest {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //复制目标(读取硬盘文件 到 内存数据)
            fis = new FileInputStream("MyJavaFileOutputStream");
            //粘贴目标(写出内存数据 到 硬盘文件)
            fos = new FileOutputStream("src/com/dhrj/zs/MyJavaFileOutputStream", true);
            //同时进行读写业务
            byte[] bytes = new byte[1024 * 1024];//1024B * 1024B = 1048576B = 1024KB =  1mb
            int readByteNum;
            while ((readByteNum = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, readByteNum);
            }
            //刷新输出流管道
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

        文件字符流案例:使用FileReader+FileWrter复制粘贴文件,如下: 

package com.dhrj.zs.file;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * created by on 2022/4/26
 * 描述:文件拷贝:使用FileReader + FileWriter
 * 一边读一边写,字符流方式仅支持拷贝普通文本文件
 *
 * @author ZSAndroid
 * @create 2022-04-26-18:02
 */
public class FileReaderWriterCopyTest {
    public static void main(String[] args) {
        FileReader fileReader = null;
        FileWriter fileWriter = null;
        try {
            fileReader = new FileReader("file");//读取输入
            fileWriter = new FileWriter("file2", true);//写出输出

            char[] chars = new char[1024 * 512]; // 1个char为2个字节,1024 * 512 = 1MB
            //同时读写业务
            int readerNum;
            while ((readerNum = fileReader.read(chars)) != -1) {
                fileWriter.write(chars, 0, readerNum);
            }
            //刷新输出流管道
            fileWriter.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

 二、转换流(字节流转字符流)
1.输入

        字节输入流转换字符输入流InputStreamReader(仅作为转换)

2.输出

        字节输出流转换字符输出流OutStreamWriter(仅作为转换)


三、缓冲流
1.字节流

(1)缓冲字节输入流BufferedInputStream(读取硬盘,写入内存)

(2)缓冲字节输出流BufferedOutputStream(读取内存、写入硬盘)

2.字符流+转换流

(1)缓冲字符输入流BufferedReader(读取硬盘,写入内存)

(2)缓冲字符输出流BufferedWriter(读取内存、写入硬盘

        缓冲区字节输入流使用代码如下: 

package com.dhrj.zs.buffer;

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * created by on 2022/4/26
 * 描述:缓冲区字节输入流
 *
 * @author ZSAndroid
 * @create 2022-04-26-19:04
 */
public class BufferInputStreamTest {
    public static void main(String[] args) {
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream("MyBufferedOutputStream"));
            byte[] bytes = {'Z', 'S', 'A', 'n', 'd', 'r', 'o', 'i', 'd'};
            bos.write(bytes);
            //刷新输出流管道
            bos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

        缓冲区字节输出流使用代码如下:

package com.dhrj.zs.buffer;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * created by on 2022/4/26
 * 描述:缓冲区字节输出流
 *
 * @author ZSAndroid
 * @create 2022-04-26-19:05
 */
public class BufferOutInputStreamTest {
    public static void main(String[] args) {
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(new FileInputStream("MyJavaFileOutputStream"));
            byte[] bytes = new byte[1024 * 1024];  //每次读取1MB
            int readByteCount;
            while ((readByteCount = bis.read(bytes)) != -1) {
                System.out.println(new String(bytes, 0, readByteCount));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

        缓冲区字符输入流 + InputStreamReader转换流(字节流 转换为 字符流)使用代码如下: 

package com.dhrj.zs.buffer;

import java.io.*;

/**
 * created by on 2022/4/26
 * 描述:缓冲区字符输入流 + InputStreamReader转换流(字节流 转换为 字符流)
 *
 * @author ZSAndroid
 * @create 2022-04-26-18:37
 */
public class BufferReaderTest {
    public static void main(String[] args) {
        BufferedReader reader = null;
        try {
            //FileInputStream 节点流 InputStreamReader 包装流 相对角度
            //InputStreamReader 节点流 BufferedReader 包装流 相对角度
            reader = new BufferedReader(new InputStreamReader(new FileInputStream("file")));
            String strResult = null;
            while ((strResult = reader.readLine()) != null) {
                System.out.println(strResult);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    //只需关闭包装流即可,底层源码自动关闭节点流
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

        缓冲区字符输出流 + OutputStreamWriter转换流(字节流 转换为 字符流) 使用代码如下: 

package com.dhrj.zs.buffer;

import java.io.*;

/**
 * created by on 2022/4/26
 * 描述:缓冲区字符输出流  + OutputStreamWriter转换流(字节流 转换为 字符流)
 *
 * @author ZSAndroid
 * @create 2022-04-26-18:46
 */
public class BufferWriterTest {
    public static void main(String[] args) {
        BufferedWriter bufferedWriter = null;
        try {
            //FileOutputStream 节点流 OutputStreamWriter 包装流 相对角度
            //OutputStreamWriter 节点流 BufferedWriter 包装流 相对角度
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("MyBufferedWriter", true)));
            bufferedWriter.write("BufferWriterTest文件写入成功");
            //刷新输出流管道
            bufferedWriter.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedWriter != null) {
                try {
                    //只需关闭包装流即可,底层源码自动关闭节点流
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 四、数据流(不常用)

1.DataInputStream(可以将非普通文本文件中的数据连同数据类型一并写入加密文件,打开乱码,只能用DataOutputStream按照写入顺序一致才能提取文件内容)

2.DataOutputStream(只能使用DataInputStream来读,读的顺序需要和写的顺序一致,才可以正常取出数据)


 五、标准输出流
1.PrintWriter 2.PrintStream

        标准输出流PrintStream:更改控制台输出方向为MyPrintStreamFile文件使用代码如下:

package com.dhrj.zs.print;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * created by on 2022/4/26
 * 描述:标准输出流:更改控制台输出方向为MyPrintStreamFile文件
 *
 * @author ZSAndroid
 * @create 2022-04-26-20:20
 */
public class PrintStreamTest {
    public static void main(String[] args) {
        LogUtil.log("使用Java标准输出流自定义日志工具类,将打印输出的信息写入log.txt文件里");
    }
}

/**
 * 自定义打印日志工具类
 */
class LogUtil {
    public static void log(String msg) {
        PrintStream printStream = null;
        try {
            //指向日志文件log.txt,自动追加
            printStream = new PrintStream(new FileOutputStream("log.txt", true));
            //改变输出方向
            System.setOut(printStream);
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
            String strTime = simpleDateFormat.format(date);
            System.out.println(strTime + ":" +msg);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (printStream != null) {
                printStream.close();
            }
        }
    }
}

六、对象流
1.ObjectInputStream 2.ObjectOutputStream

******学习记录,不喜勿喷****** 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/737611.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-28
下一篇 2022-04-28

发表评论

登录后才能评论

评论列表(0条)

保存