IO流----节点流/文件流

1.FileReader/FileWriter字符流的使用

2.FileInputStream / FileOutputStream字节流的使用
/*

1.1 FileReader的使用
将当前Module下的hello.txt文件内容读入程序中,并输出到控制台

说明点:
1. read()的理解:返回读入的一个字符。如果达到文件末尾,返回-1
2. 异常的处理:为了保证流资源一定可以执行关闭操作。需要使用try-catch-finally处理
3. 读入的文件一定要存在,否则就会报FileNotFoundException。

package main.IO.Characterstream;

import org.junit.Test;

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

/**
 * @Author lx
 * @Description 将硬盘中的文件读入到内存中
 * @Date 15:57 2020/8/18
 * @Version
 */
public class ReadTest {

    //将文件读入到内存中的条件时 文件必须存在
    @Test
    public void test01(){
        FileReader reader = null;
        try {
            File file = new File("hello.txt");
            reader = new FileReader(file);
            int data;
            while ((data=reader.read())!=-1){
                System.out.print((char) data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

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

    //运用字符数组进行读取
    @Test
    public void test02(){
        FileReader reader = null;
        try {
            //1.File类的实例化
            File file = new File("hello.txt");

            //2.FileReader流的实例化
            reader = new FileReader(file);

            //2.读入操作
            char[] cbuf = new char[5];
            int len;
            //len接收每次读入了几个数据
            //read(char[] cbuf):返回每次读入cbuf数组中的字符的个数。如果达到文件末尾,返回-1
            while ((len = reader.read(cbuf))!=-1){
                for (int i=0 ; i<len; i++){
                    System.out.print(cbuf[i]);
                }
                System.out.print(" "+len+"\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader!=null) {
                try {
                    //资源的关闭
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

}

1.2 FileWriter的使用
/*
从内存中写出数据到硬盘的文件里。

说明:
1. 输出操作,对应的File可以不存在的。并不会报异常
2.
File对应的硬盘中的文件如果不存在,在输出的过程中,会自动创建此文件。
File对应的硬盘中的文件如果存在:
如果流使用的构造器是:FileWriter(file,false) / FileWriter(file):对原文件的覆盖
如果流使用的构造器是:FileWriter(file,true):不会对原文件覆盖,而是在原文件基础上追加内容

*/

@Test
public void testFileWriter() {
    FileWriter fw = null;
    try {
        //1.提供File类的对象,指明写出到的文件
        File file = new File("hello1.txt");

        //2.提供FileWriter的对象,用于数据的写出
        fw = new FileWriter(file,false);

        //3.写出的操作
        fw.write("I have a dream!\n");
        fw.write("you need to have a dream!");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        //4.流资源的关闭
        if(fw != null){

            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

1.3FileReader/FileWriter实现文本文件的复制

 

  @Test
    public void test()  {
        FileReader reader = null;
        FileWriter writer = null;
        try {
            //1、创建file类对象,指明读入和写入的文件
            File srcFile = new File("hello.txt");
            File destFile = new File("hello2.txt");

            //2、创建输入流和输出流对象
            reader = new FileReader(srcFile);
            writer = new FileWriter(destFile);

            //3、数据的读取和写入操作
            char[] cbuf = new char[5];
            int len;
            while ((len=reader.read(cbuf))!=-1){
                writer.write(cbuf,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader!=null && writer!=null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

 

2.FileInputStream / FileOutputStream的使用:
* 1. 对于文本文件(.txt,.java,.c,.cpp),使用字符流处理
* 2. 对于非文本文件(.jpg,.mp3,.mp4,.avi,.doc,.ppt,...),使用字节流处理
/*
实现对图片的复制操作
*/

@Test
    public void test02() {
        FileInputStream InputStream= null;
        FileOutputStream outputStream = null;
        try {
            //1.造文件
            File srcFile = new File("1.jpg");
            File destFile = new File("2.jpg");

            //2.造流
            InputStream = new FileInputStream(srcFile);
            outputStream = new FileOutputStream(destFile);

            //3.复制过程
            byte[] bytes=new byte[5];
            int len;
            while ((len = InputStream.read(bytes))!=-1){
                outputStream.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (InputStream!=null) {
                try {
                    InputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

 

posted @ 2020-08-18 23:39  lixia64  阅读(147)  评论(0编辑  收藏  举报