JavaSE-IO流--IO原理、流的分类、FileReader、FileWriter

1、IO原理及流的分类

1.1、Java IO原理

在这里插入图片描述
在这里插入图片描述

1.2、流的分类

在这里插入图片描述

1.3、IO流体系

在这里插入图片描述

2、数据读入FileReader

2.1、FileReader读入数据的基本操作:使用read()方法

  • IDEA中main方法中文件路径是相对于当前工程
  • IDEA中单元测试中的该文件路径是相对于当前module
package com.io.test;

import org.junit.Test;

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

/**
 * 一、流的分类
 * 1、操作数据单位:字节流、字符流
 * 2、数据的流向:输入流、输出流
 * 3、流的角色:节点流、处理流
 * 二、流的体系结构
 * 抽象基类              节点流(文件流)           缓冲流(处理流的一种)
 * InputStream          FileInputStream         BufferedInputStream
 * OutputStream         FileOutStream           BufferedOutputStream
 * Reader               FileReader              BufferedReader
 * Writer               FileWriter              BufferedWriter
 */
public class FileReaderWriterTest {

    public static void main(String[] args) {
        File file = new File("Hello.txt");//main方法中该文件路径是相对于当前工程
        System.out.println(file.getAbsolutePath());//获取Hello.txt文件的绝对路径:E:\workspaces\IDEA\JavaSE\Hello.txt

        File file1 = new File("IO\\Hello.txt");
        System.out.println(file1.getAbsolutePath());
    }

     /**
     * 将Hello.txt文件内容读入到程序中,并输出到控制台.
     * 说明点:
     *  1、read()的理解:返回读入的一个字符。如果达到文件末尾,返回-1
     *  2、异常的处理:为了保证资源一定可以执行关闭操作。需要使用try-catch-finally处理
     *  3、读入的文件一定要存在,否则就会报FileNotFoundException
     */
    @Test
    public void test(){
        FileReader fileReader = null;
        try {
            //1、实例化File对象,指明要操作的文件
            File file = new File("Hello.txt");//单元测试中的该文件路径是相对于当前module
            //System.out.println(file.getAbsolutePath());//控制台输出为:E:\workspaces\IDEA\JavaSE\IO\Hello.txt

            //2、提供具体的流
            fileReader = new FileReader(file);

            //3、数据的读入
            //Read():返回读入的一个字符。如果达到文件末尾,返回-1
            int data = fileReader.read();
            while (data != -1){
                System.out.print((char)data);
                data = fileReader.read();
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally{
            try {
                //4、关闭流
                if(fileReader != null){
                    fileReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

在这里插入图片描述

2.2、FileReader使用read(char[] cbuf)读入数据

  • read(char[] cbuf):返回每次读入到数组中的字符个数,
public class FileTest{
//对read()操作升级:使用read的重载方法
    @Test
    public void testFileReader(){
        FileReader fr = null;
        try {
            //1、File类的实例化
            File file = new File("Hello.txt");
            //2、FileReader流的实例化
            fr = new FileReader(file);
            //3、读入的操作
            //read(char[] cbuf):返回每次读入cbuf数组中字符的个数,如果达到文件末尾,返回-1
            char[] cbuf = new char[5];
            int len;
            while((len = fr.read(cbuf)) != -1){
                //方式一
                for(int i = 0; i < len; i++){
                    System.out.print(cbuf[i]);
                }
                //方式二
//                String str = new String(cbuf,0,len);
//                System.out.print(str);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            //4、关闭流
            if(fr !=null){
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
 }

在这里插入图片描述

3、写出数据FileWriter

/**
 * 从内存中写出数据到硬盘的文件里。
 * 说明:
 * 1、输出操作,对应的File可以不存在的。
 * 2、File对应的硬盘中的文件如果不存在,在输出的过程中,会自动创建文件。
 *    File的硬盘中的文件如果存在:
 *          如果流使用的构造器是:FileWriter(file,false)/FileWriter(file):对原有文件覆盖
 *          如果流使用的构造器是:FileWriter(file,true):不会对原有的文件覆盖,而是在原有文件基础上增加
 */
public class FileWriter {
    @Test
    public void test() throws IOException {
        //1、提供File类的对象,指明写出到的文件
        File file = new File("writer.txt");

        //2、提供FileWriter的对象,用于数据的写出
        java.io.FileWriter fileWriter = new java.io.FileWriter(file,true);

        //3、写出的操作
        fileWriter.write("I have a dream!\n");
        fileWriter.write("You need to have a dream!\n");

        //4、流资源的关闭
        fileWriter.close();

    }
}

在这里插入图片描述

4、FileReader和FileWriter实现文本文件的复制

 //FileReader和FileWriter实现文本文件的复制
    @Test
    public void test01()  {
        FileReader fr = null;
        FileWriter fw = null;
        try{
            //1、提供File类的对象,指明读入和写出的文件
            File srcFile = new File("hello.txt");
            File destFile = new File("hello1.txt");

            //2、提供输入流和输出流的对象
            fr = new FileReader(srcFile);
            fw  =new FileWriter(destFile);

            //3、数据的读入和写出的操作
            char[] cbuf = new char[5];
            int len;//记录每次读入到cbuf数组中字符的个数
            while ((len = fr.read(cbuf)) != -1){
                fw.write(cbuf,0,len);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            //4、流资源的关闭
            if(fw!=null){
                //4、流资源的关闭
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    if(fr!=null){
                        try {
                            fr.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

5、FileInputStream、FileOutputStream实现非文本文件的复制

  • 对于文本文件(.txt .java .c .cpp)使用字符流处理
  • 对于非文本文件(.jpg .mp3 .mp4 .avi .doc .ppt)使用字符流处理

5.1、图片的复制

public class FileInputOuputStream {
    //实现图片(非文本文件)的的复制
    @Test
    public void test01()  {
        FileInputStream fr = null;
        FileOutputStream fw = null;
        try{
            //1、提供File类的对象,指明读入和写出的文件
            File srcFile = new File("bird.png");
            File destFile = new File("bird1.png");

            //2、提供输入流和输出流的对象
            fr = new FileInputStream(srcFile);
            fw  =new FileOutputStream(destFile);

            //3、数据的读入和写出的操作
            byte[] cbuf = new byte[5];
            int len;//记录每次读入到cbuf数组中字符的个数
            while ((len = fr.read(cbuf)) != -1){
                fw.write(cbuf,0,len);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            //4、流资源的关闭
            if(fw!=null){
                //4、流资源的关闭
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    if(fr!=null){
                        try {
                            fr.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}

在这里插入图片描述

5.2、视频的复制

public class FileInputOuputStream {
	public void copyFile(String srcFile,String destFile)  {
	        FileInputStream fis = null;
	        FileOutputStream fos = null;
	        try{
	            //2、提供输入流和输出流的对象
	            fis = new FileInputStream(srcFile);
	            fos  =new FileOutputStream(destFile);
	
	            //3、数据的读入和写出的操作
	            byte[] cbuf = new byte[1024];
	            int len;//记录每次读入到cbuf数组中字符的个数
	            while ((len = fis.read(cbuf)) != -1){
	                fos.write(cbuf,0,len);
	            }
	        }catch (IOException e){
	            e.printStackTrace();
	        }finally {
	            //4、流资源的关闭
	            if(fos!=null){
	                //4、流资源的关闭
	                try {
	                    fos.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }finally {
	                    if(fis!=null){
	                        try {
	                            fis.close();
	                        } catch (IOException e) {
	                            e.printStackTrace();
	                        }
	                    }
	                }
	            }
	        }
	    }
	
	    @Test
	    public void test(){
	        String srcFile = "C:\\Users\\Lenovo\\Desktop\\01-视频.mp3";
	        String destFile = "C:\\Users\\Lenovo\\Desktop\\02-视频.mp3";
	        long start = System.currentTimeMillis();
	        copyFile(srcFile, destFile);
	        long end = System.currentTimeMillis();
	        System.out.println("复制视频耗时:" + (end-start) );
	    }
	}

6、字节型缓冲流的使用BufferedInputStream 和BufferedOutputStream

6.1、复制文本

 public class BufferedTest {
    @Test
    public void test(){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //1、造文件
            File srcFile = new File("bird.png");
            File destFile = new File("bird3.png");
            //2、造流
            //2.1、造节点流
            FileInputStream fis = new FileInputStream(srcFile);
            FileOutputStream fos = new FileOutputStream(destFile);
            //2.2、造缓冲流
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            //3、复制的细节:读取,写入
            byte[] buffer = new byte[5];
            int len;
            while((len = bis.read(buffer)) != -1){
                fos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {//说明:关闭外层流的同时,内层流也会自动的进行关闭。关于内层流的关闭。我们可以省略
            if(bos != null){
                //4、资源关闭
                //要求:先关闭外层的流、再关闭内层的流
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bis != null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
 }

6.2、复制视频

 public class BufferedTest {
  public void copyFile(String srcFile,String destFile)  {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try{
            //2、提供输入流和输出流的对象
            fis = new FileInputStream(srcFile);
            fos  =new FileOutputStream(destFile);

            //3、数据的读入和写出的操作
            byte[] cbuf = new byte[1024];
            int len;//记录每次读入到cbuf数组中字符的个数
            while ((len = fis.read(cbuf)) != -1){
                fos.write(cbuf,0,len);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            //4、流资源的关闭
            if(fos!=null){
                //4、流资源的关闭
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    if(fis!=null){
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

		//测试方法
    @Test
    public void test(){
        String srcFile = "C:\\Users\\Lenovo\\Desktop\\01-视频.mp3";
        String destFile = "C:\\Users\\Lenovo\\Desktop\\02-视频.mp3";
        long start = System.currentTimeMillis();
        copyFile(srcFile, destFile);
        long end = System.currentTimeMillis();
        System.out.println("复制视频耗时:" + (end-start) );
    }
 }

7、字节型缓冲流的使用BufferedReader和BufferedWriter

@Test
    public void testBufferedReaderBufferWriter(){
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            //创建文件和相应的流
            br = new BufferedReader(new FileReader(new File("hello.txt")));
            bw = new BufferedWriter(new FileWriter(new File("hello.txt")));
            //读写操作
            //方式一:使用char[]数组
//            char[] cbuff = new char[5];
//            int len;
//            while((len = br.read(cbuff)) != -1){
//                bw.write(cbuff,0,len);
//            }
            //方式二:使用String[]
            String data;
            while((data=br.readLine()) !=null){
                //方法一:
                bw.write(data);//data中不包含换行符
                //方法二:
                bw.write(data);//data中不包含换行符
                bw.newLine();//提供换行操作

            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {//关闭资源
            if(bw != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
           if(br != null){
               try {
                   bw.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
        }
    }

8、总结

 二、流的体系结构
 抽象基类          节点流(文件流)                                 缓冲流(处理流的一种)
 InputStream    FileInputStream (read(byte[] buffer)          BufferedInputStream (read(byte[] buffer)
 OutputStream   FileOutStream (write(byte[] buffer,0,len))    BufferedOutputStream (write(byte[] buffer,0,len))
 Reader         FileReader (read(byte[] buffer)               BufferedReader (read(byte[] buffer /readLine())
 Writer         FileWriter (write(byte[] buffer,0,len))       BufferedWriter (write(byte[] buffer,0,len))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值