JavaSE笔记20:IO流(一)

28 篇文章 0 订阅

IO流(一)

下一篇:IO流(二)

概述

在这里插入图片描述

I:Input

O:Output

通过IO可以完成硬盘文件的读和写

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

流的分类

分类方式:

  1. 按照流的方向进行分类:

    (1)以内存作为参照物,往内存中去,叫做输入(Input),或者叫做读(Read)

    (2)从内存中出来,叫做输出(Output),或者叫做写(Write)

  2. 按照读取数据方式不同进行分类

    (1)有的流按照字节的方式读取数据,一次读取一个字节,等同于一次读取8个二进制位

    这种流是万能的,什么类型的文件都可以读取。

    ​ 假设文件file.txt,采用字节流的话是这样读的:

    ​ a中国bc张三fe

    ​ 第一次读:一个字节,正好读到’a’

    ​ 第二次读:一个字节,正好读到‘中’字符的一半

    ​ 第三次读:一个字节,正好读到‘中’字符的另外一半

    (2)有的流是按照字符的方式读取数据的,一次读取一个字符,这种流是为了方便读取普通文本文件而存在的,这种流不能读取:图片、声音、视频等文件,只能读取纯文本文件,连word文件都无法读取。

    ​ 假设文件file.txt,采用字符流的话是这样读的:

    ​ a中国bc张三fe

    ​ 第一次读:‘a’字符(‘a’字符在Windows系统中占用1个字节)

    ​ 第二次读:‘中’字符(‘中’字符在Windows系统中占用2个字节)

综上所述,流的分类有:输入流、输出流、字节流、字符流

流的四大家族

四大家族的首领:

  1. java.io.InputStream 字节输入流
  2. java.io.OutpuStream 字节输出流
  3. java.io.Reader 字符输入流
  4. java.io.Writer 字符输出流

四大家族的首领都是抽象类(abstract class)

注意:在java中只要“类名”以Stream结尾的都是字节流。以“Reader/Writer”结尾的都是字符流

流的close和flush方法

  1. 所有的流都实现了:java.io.Closeable接口,都是可以关闭的,都有close()方法

    养成好习惯:避免造成耗费资源,用完流一定要关闭

  2. 所有的输出流都实现了:java.io.Flushable接口,都是可刷新的,都有flush()方法

    养成好习惯:输出流在最终输出之后,一定要记得flush()刷新,刷新的作用就是清空管道

    注意:如果没有flush()可能会导致丢失数据

java.io包下需要掌握的流有个16:

文件专属:

  1. java.io.FileInputStream

  2. java.io.FIleOutputStream

  3. java.io.FileReader

  4. java.io.FileWriter

转换流:

  1. java.io.InputStreamReader
  2. java.io.OutputStreamWriter

缓冲流专属:

  1. java.io.BufferedReader
  2. java.io.BufferedWriter
  3. java.io.BufferedInputStream
  4. java.io.BufferedOutputStream

数据流专属:

  1. java.io.DataInputStream
  2. java.io.DataOutputStream

标准输出流:

  1. java.io.PrintWriter
  2. java.io.PrintStream

对象专属流:

  1. java.io.ObjectInputStream
  2. java.io.ObjectOutputStream

FileInputStream

package se4.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
 * java.io.FIleInputStream:
 * 1.文件字节输入流,万能的,任何类型的文件都可以采用这个流来读
 * 2.字节的方式,完成输入的操作,完成读的操作(硬盘--->内存)
 */
public class FileInputStreamTest01 {
    public static void main(String[] args) {
        //创建文件字节输入流对象
        FileInputStream fis = null;
        try {
            //以下都是采用绝对路径的方式
            //文件路径:E:\学习资料\老杜Java系列\File(IDEA会自动把\变成\\,因为java中\表示转义)
            fis = new FileInputStream("E:\\学习资料\\老杜Java系列\\File\\temp.txt");
            //开始读,temp.txt文件内容:abcdef
            int readData = fis.read();//这个方法的返回值是:读取到的“字节”本身
            System.out.println(readData);//97
            int readData2 = fis.read();
            System.out.println(readData2);//98
            int readData3 = fis.read();
            System.out.println(readData3);//99
            int readData4 = fis.read();
            System.out.println(readData4);//100
            int readData5 = fis.read();
            System.out.println(readData5);//101
            int readData6 = fis.read();
            System.out.println(readData6);//102
            //已经读到文件的末尾了,再读的时候读取不到任何数据,返回-1
            int readData7 = fis.read();
            System.out.println(readData7);//-1
            int readData8 = fis.read();
            System.out.println(readData8);//-1

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //在finally语句块当中确保流一定关闭
            if (fis == null) {//避免空指针异常
                //关闭流的前提是:流不是空的。流是null的时候没必要关闭
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
package se4.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
//对上一个程序进行改进
public class FileInputStreamTest02 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("E:\\学习资料\\老杜Java系列\\File\\temp.txt");
           /*
           while (true){
                int readData = fis.read();
                if (readData == -1){
                    break;
                }
                System.out.print(readData + " ");
            }//输出结果:97 98 99 100 101 102
           */
            //改造while循环
            int readData = 0;
            while ((readData = fis.read()) != -1){
                System.out.print(readData + " ");
            }//输出结果:97 98 99 100 101 102

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

分析以上两个程序的缺点:一次读取一个字节byte,这样内存和硬盘交互太频繁,基本上时间/资源都耗费在交互上面了。(不需要掌握上面的两个程序)

package se4.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
 * int read(byte[] b)
 * 一次最多读取b.length个字节
 * 减少硬盘和内存的交互,提高程序的执行效率
 * 往byte[]数组当中读
 */
public class FileInputStreamTest03 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("E:\\学习资料\\老杜Java系列\\File\\temp.txt");
            //开始读,采用byte数组,一次读取多个字节,最多读取数组.length个字节
            byte[] bytes = new byte[4];//准备一个4个长度的byte数组,一次最多读取4个字节
            //这个方法的返回值是:读取到的是字节数量(不是字节本身)
            int readCount = fis.read(bytes);
            //第一次读到了4个字节
            System.out.println(readCount);//4
            //下一行是将字节数组全部转换成字符串
            //System.out.println(new String(bytes));//abcd
            //不应该全部转换,应该是读取到了多少个字符就转换多少个,修改如下
            System.out.println(new String(bytes,0,readCount));//abcd

            //第二次只能读到2个字节
            readCount = fis.read(bytes);
            System.out.println(readCount);//2
            //System.out.println(new String(bytes));//efcd
            //不应该全部转换,应该是读取到了多少个字符就转换多少个,修改如下
            System.out.println(new String(bytes,0,readCount));//ef

            //1个字节都没有读到,返回-1
            readCount = fis.read(bytes);
            System.out.println(readCount);//-1

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

最终版,需要掌握

package se4.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/*
最终版,需要掌握
 */
public class FileInputStreamTest04 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("E:\\学习资料\\老杜Java系列\\File\\temp.txt");
            //准备一个byte数组
            byte[] bytes = new byte[4];
            /*太low,不建议这样写
            while (true){
                int readCount = fis.read(bytes);
                if (readCount == -1){
                    break;
                }
                //把byte数组转换成字符串,读到多少个转换多少个
                System.out.print(new String(bytes,0,readCount));//abcdef
            }*/

            int readCount = 0;
            while ((readCount = fis.read(bytes)) != -1){
                System.out.print(new String(bytes,0,readCount));
            }//输出结果:abcdef

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

FileInputStream类的其他常用方法

int available();
package se4.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/*
FileInputStream类的其他常用方法
    int available():返回流当中剩余的没有读到的字节数量
    long skip(long n):跳过几个字节不读
temp.txt文件内容为:abcdef
 */
public class FileInputStreamTest05 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("E:\\学习资料\\老杜Java系列\\File\\temp.txt");
            System.out.println("总字节数量:" + fis.available());//6
            //读1个字节
            //int readByte = fis.read();
            //还剩下可以读的字节数量是:5
            //System.out.println("剩下" + fis.available() + "个字节没有读");
            //这个方法有什么用?如下
            byte[] bytes = new byte[fis.available()];//这种方式不适合太大的文件,因为byte数组不能太大
            //不需要循环了
            //直接读一次就行了
            int readCount = fis.read(bytes);//6
            System.out.print(new String(bytes));//abcdef

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis == null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
long skip(long n);
package se4.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/*
FileInputStream类的其他常用方法
    int available():返回流当中剩余的没有读到的字节数量
    long skip(long n):跳过几个字节不读
temp.txt文件内容为:abcdef
 */
public class FileInputStreamTest06 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("E:\\学习资料\\老杜Java系列\\File\\temp.txt");
            System.out.println("总字节数量:" + fis.available());//6
            //skip跳过几个字节不读取
            //跳过3个字节
            fis.skip(3);
            System.out.println(fis.read());//100

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

FileOutputStream

package se4.io;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
 * 文件字节输出流,负责写
 * 从内存到硬盘
 */
public class FileOutputStreamTest01 {
    public static void main(String[] args) {
        FileOutputStream fos = null;
        try {
            //myfile文件不存在的时候会自动新建!
            //这种方式谨慎使用,这种方式会先将原文件清空,然后重新写入
            fos = new FileOutputStream("E:\\学习资料\\老杜Java系列\\File\\myfile.txt");
            //开始写
            byte[] bytes = {97,98,99,100};
            //将byte数组全部写出!
            fos.write(bytes);//abcd
            //将byte数组的一部分写出!
            fos.write(bytes,0,2);//再写出ab

            //以追加的方式在文件末尾写入,不会清空原文件的内容
            fos = new FileOutputStream("E:\\学习资料\\老杜Java系列\\File\\myfile.txt",true);
            //字符串
            String s = "卧槽~";
            //将字符串转换成byte数组
            byte[] bs = s.getBytes();
            fos.write(bs);//不会清空前面的内容,会在后面加上卧槽~

            //写完之后,最后一定要刷新
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos == null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

文件的拷贝

package se4.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
 * 使用FileInputStream和FileOutputStream完成文件的拷贝
 * 拷贝过程应该是一边读、一边写
 * 使用以上的字节流拷贝文件的时候,文件类型随意,万能的,什么样的文件都能拷贝
 */
public class Copy01 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //创建一个输入流
            fis = new FileInputStream("E:\\download\\music\\虞兮叹.mp3");
            //创建一个输出流
            fos = new FileOutputStream("E:\\学习资料\\老杜Java系列\\File\\虞兮叹.mp3");

            //最核心:一边读,一边写
            byte[] bytes = new byte[1024 * 1024];//1MB(一次最多拷贝1MB)
            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 {
            //分开try,不要一起try
            //一起try的时候,其中一个出现异常可能会影响到另一个流的关闭
            if (fis == null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos == null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

FileReader

package se4.io;

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

/**
 * FIleReader:
 *  文件字符输入流,只能读取普通文本
 *  读取文本内容时,比较方便、快捷
 */
public class FileReaderTest {
    public static void main(String[] args) {
        FileReader reader = null;
        try {
            //创建文件字符输入流
            reader = new FileReader("E:\\学习资料\\老杜Java系列\\File\\temp.txt");
            //开始读
            char[] chars = new char[4];//一次读取4个字符
            int readCouunt = 0;
            while ((readCouunt = reader.read(chars)) != -1){
                System.out.print(new String(chars,0,readCouunt));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader == null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

FileWriter

package se4.io;

import java.io.FileWriter;
import java.io.IOException;
/**
 * FileWriter:
 *  文件字符输出流,写
 *  只能输出普通文本
 */
public class FileWriterTest {
    public static void main(String[] args) {
        FileWriter out = null;
        try {
            //创建文件字符输出流对象
            out = new FileWriter("E:\\学习资料\\老杜Java系列\\File\\myfile2.txt");
            //out = new FileWriter("E:\\学习资料\\老杜Java系列\\File\\myfile2.txt",true);不清空,在末尾追加

            //开始写
            char[] chars = {'我','是','中','国','人'};
            out.write(chars);//我是中国人
            out.write(chars,2,3);//中国人
            out.write("\n");//换行符
            out.write("我是一名java软件工程师!");//我是一名java软件工程师!

            //刷新
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

普通文本文件的拷贝

package se4.io;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
 * 使用FileReader和FileWriter拷贝
 * 只能拷贝“普通文本”文件
 */
public class Copy02 {
    public static void main(String[] args) {
        FileReader in = null;
        FileWriter out = null;
        try {
            //读
            in = new FileReader("C:\\Users\\QD\\Desktop\\个人博客地址.txt");
            //写
            out = new FileWriter("E:\\学习资料\\老杜Java系列\\File\\个人博客地址.txt");
            //一边读、一边写
            char[] chars = new char[1024 * 512];//1MB
            int readCount = 0;
            while ((readCount = in.read(chars)) != -1){
                out.write(chars,0,readCount);
            }
            //刷新
            out.flush();
        } catch (FileNotFoundException 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();
                }
            }
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值