IO流-笔记

io流

io流可以分为输入流(input)和输出流(output),往内存中去就是读(read),从内存中出来就是写(write).

也可以分为字节流和字符流.

​ 字节流按照字节的方式读取,一次读一个字节,这种方式比较万能,什么类型的文件都可以读取,包括:文本文档,图片,声音文件,视频.

​ 字符流按照字符的方式读取,这种是未来方便读取普通的文本文档,不能读取图片,视频,声音文件等等.连word文件都无法读取

所以,io流的分类:

​ 输入流,输出流.

​ //字节流,字符流.(字节流读取的是8个二进制位,字符流可以一个字符一个字符的检测出来)

java中所有的流都是在: java.io.*; 里面.

java.io.*中有四大家族:

​ java.io.InputStream 字节输入流

​ java.io.OutputStream 字节输出流

​ java.io.Writer 字符输出流

​ java.io.Reader 字符输入流

​ 这四大家族都是抽象类.(abstracr class)

​ 所有的流都实现了java.io.Closeable的接口,都可以关闭,都有close方法.用完流后最好要关闭,不然会占据很多资源.

​ java中所有的类名是Stream结尾都是字节流,以Reader 和 Writer 结尾的都是字符流.

​ 所有的输出流都实现了 java.io.Flushable 的接口,所以都是可刷新的,都有 flush() 方法,输出流输出后,最好 flush() 方法刷新一下,目的是让管道内的数据都都传入到文件中(清空管道!),如果没有用的话,可能会导致数据丢失.

java.io中需要掌握的16个流:

文件专属:

​ java.io.FileInputStream (重要)

​ java.io.FileOutputStream (重要)

​ java.io.FileReader

​ java.io.FileWriter

转换流: (将字节流转换为字符流)

​ java.io.InputStreamReader

​ java.io.OutputStreamWriter

缓冲流专属:

​ java.io.BufferedInputStream

​ java.io.BufferedOutputStream

​ java.io.BufferedReader

​ java.io.bufferedWriter

数据流专属:

​ java.io.DataInputStream

​ java.io.DataOutputStream

标准输出流:

​ java.io.PrintWriter (重要)

​ java.io.PrintStream

对象专属流:

​ java.io.ObjectInputStream (重要)

​ java.io.ObjectOutputStream (重要)

java.io.FileInputStream

package io;

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

public class FileInputStreamTest {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            // 创建对象
            fis = new FileInputStream("D:\\桌面\\目标.txt");
            // 创建一个byte数组存放数据
            byte[] readRes = new byte[fis.available()];
            // 读取数据,一次读取一个
            int readCount = fis.read(readRes);
            // 输出,从0开始,从最后一个结束
            System.out.println(new String(readRes, 0, readCount));

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

java.io.FileOutputStream

package io;

import sun.net.www.protocol.http.HttpURLConnection;

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

public class FileOutputStreamTest {
    public static void main(String[] args) {
        FileOutputStream fos = null;
        try {
            // 创建对象
            fos = new FileOutputStream("tempfile1", true);
            // 创建一个byte数组
            byte[] bytes = {97, 98, 99, 100};
            // 定义一个字符串
            String a = "我是一个中国人,我骄傲!!!";
            // 字符串转化为byte数组
            byte[] bs = a.getBytes();
            // 写入数据
            fos.write(bs);
            // 清空管道
            fos.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fos != null);{
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

java.io.FileReader

package io;

import javax.smartcardio.ResponseAPDU;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class FileReaderTest {
    public static void main(String[] args) {
        FileReader reader = null;
        try {
            // 创建对象
            reader =  new FileReader("tempfile1");
            // 创建char数组来存储数据
            char[] chars = new char[3];
            int readCount = 0;
            // 读取数据
            while ((readCount = reader.read(chars)) != -1){
                System.out.println(new String(chars, 0, readCount));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

java.io.FileWriter

package io;

import java.io.FileWriter;
import java.io.IOException;
import java.lang.invoke.WrongMethodTypeException;

public class FileWriterTest {
    public static void main(String[] args) {
        FileWriter writer = null;
        try {
            // 创建对象
            writer = new FileWriter("tempfile1", true);
            // 定义字符串
            String a = "\n世界还很大,目标还很远,加油!!!";
            char[] chars = {'加', '油'};
            // 写入字符串
            writer.write(chars);
            // 清空管道
            writer.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (writer != null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

java.io.ObjectInputStream

package io;

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.List;

public class ObjectInputStreamTest {
    public static void main(String[] args) throws Exception{
        // 创建对象
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("tempfile6"));

        Object obj = ois.readObject();

        List<Student> all = (List<Student>)obj;
        //for循环读取list
        for (Student i : all){
            System.out.println(i);
        }

        ois.close();

    }
}

java.io.ObjectOutputStream

package io;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;

// 参与序列化和发序列化的对象必须实现Serializable这个接口, 否则会报错
/* 通过源码发现,这个Serializable接口只是一个标志接口
    public interface Serializable {
    }
    什么都没有,起到一个标识的作用,java虚拟机看到这个类实现了这个接口,可能会给这个类一些特殊待遇
    java虚拟机看到这个接口后,会生成一个序列化版本号.
 */


public class ObjectOutputStreamTest {
    public static void main(String[] args) throws Exception{
        ArrayList<Student> allStudent = new ArrayList<>();
        allStudent.add(new Student("张三", 19));
        allStudent.add(new Student("李四", 19));
        allStudent.add(new Student("王五", 13));
        ObjectOutputStream obj = new ObjectOutputStream(new FileOutputStream("tempfile6"));

        obj.writeObject(allStudent);

        obj.flush();

        obj.close();
    }
}

复制文件

package io;

import javax.sql.rowset.BaseRowSet;
import java.awt.image.RescaleOp;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Copy {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            fis = new FileInputStream("E:\\python学习视频\\3、Python之爬虫开发帝王-video\\03-http抓包分析.wmv");
            fos = new FileOutputStream("E:\\03-http抓包分析.wmv");

            byte[] bytes = new byte[1024 * 1024];

            int readCount = 0;

            while ((readCount = fis.read(bytes)) != -1){
                fos.write(bytes, 0, readCount);
            }

            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 {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

复制文件夹(难度较大)

package io;

import javax.sound.midi.Soundbank;
import java.io.*;

public class CopyAll {
    public static void main(String[] args) {
        File srcFile = new File("E:\\Copytest\\a");
        File destFile = new File("E:\\Copytest\\b");
        CopyDir(srcFile, destFile);
    }

    private static void CopyDir(File srcFile, File destFile) {
        if (srcFile.isFile()){
            FileInputStream in = null;
            FileOutputStream out = null;
            try {
                in = new FileInputStream(srcFile);

                String destPath = destFile.getAbsolutePath() + srcFile.getAbsolutePath().substring(13);

                out = new FileOutputStream(destPath);

                byte[] bytes = new byte[1024 * 1024];

                int readCount = 0;

                while ((readCount = in.read(bytes)) != -1){
                    out.write(bytes, 0, readCount);
                }

                try {
                    out.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if (in != null){
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (out != null){
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return;
        }

        File[] files = srcFile.listFiles();

        for (File file: files){
            if (file.isDirectory()){
                String srcDir = srcFile.getAbsolutePath();
//                System.out.println(srcDir.substring(3));
                String destDir = destFile.getAbsolutePath() + srcDir.substring(13);
                File file1 = new File(destDir);
                if (!file1.exists()){
                    file1.mkdirs();
                }
            }
            CopyDir(file, destFile);
        }
    }
}
posted @ 2021-05-02 21:52  Wwxs  阅读(116)  评论(0)    收藏  举报