IO流之节点流(文件流)

2020-12-30  本文已影响0人  冯宏志

IO流的分类

按操作的数据单位:字节流(8bit),字符流(16bit)---->对于文本文件使用字符流处理,对于非文本文件,使用字节流处理
按数据的流向:输入流,输出流
按流的角色:节点流(文件流),处理流


抽象基类
抽象基类 节点流(文件流) 缓冲流(处理流的一种)
InputStream FileInputStream(read(byte[] buffer)) BufferedInputStream (read(byte[] buffer))
OutputStream FileOutputStream(write(byte[] buffer,0,len)) BufferedOutputStream (write(byte[] buffer,0,len)/flush())
Reader FileReader(read(char[] cbuf)) BufferedReader(read(char[] cbuf)/readLine())
Writer FileWriter(write(char[] cbuf,0,len)) BufferedWriter(write(char[] cbuf,0,len)/flush())

FileReader的使用

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

public static void main(String[] args) throws IOException {
        FileReader fr = null;
        try {
            //1、实例化File类的对象,指明要操作的文件
            File file1 = new File("day08\\hello.txt");//在main方法里的相对路径是相对于当前工程javasenior,在@Test里是相对当前MOdule

            //2、提供具体的流
            fr = new FileReader(file1);

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

    }
     //read()的改进用法:每次取出固定长度的char[],而非之前每次读一个char
    @Test
    public void test(){
        FileReader fr = null;
        try {
            File file1 = new File("hello.txt");
            fr = new FileReader(file1);
            char[] cbuf = new char[5];
            int len;
            while((len = fr.read(cbuf)) != -1){
                //方式一:错误的写法
//                for(int i = 0;i < cbuf.length;i++){
//                    System.out.print(cbuf[i]);//"hello world!orl":错误原因:每次输出固定长度为5的字符串,如果文件字符数不是5的倍数就不对
//                }
                //方式一:正确的写法
                for(int i = 0;i < len;i++){
                    System.out.print(cbuf[i]);//"hello world!"
                }
                //方式二:错误的写法
//                String str = new String(cbuf);
//                System.out.print(str);//"hello world!orl"错误原因同上
                //方式二:正确的写法
                String str = new String(cbuf,0,len);
                System.out.print(str);//"hello world!"
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fr != null)
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }

    }

FileWriter的使用

说明
1、输出操作,对应的File可以不存在,并不会报异常
如果不存在:在写出的过程中,会自动创建文件;
如果存在:
--------------->①如果流使用的构造器是FileWriter(file,false)/FileWriter(file):对原有文件进行覆盖
--------------->②如果流使用的构造器是FileWriter(file,true):不会对原有文件进行覆盖,而是在其基础上继续写入。

    @Test
    public void test1(){
        FileWriter fw = null;
        try {
            //1、提供File类的对象,指明写出到的文件
            File file1 = new File("hello1.txt");
            //2、提供FileWriter的对象,用于数据的写出
            fw = new FileWriter(file1);
            //3、写出的操作
            fw.write("I am a sb\n");
            fw.write("I am a nc");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4、;流资源的关闭
            if(fw != null)
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }

使用FileReader和FileWriter实现文本文件的复制

    @Test
    public void test2(){
        FileReader fr = null;
        FileWriter fw = null;
        try {
            //1、创建File类的对象,指明读入和写出的文件
            File srcFile = new File("hello.txt");
            File destFile = new File("hello2.txt");
            //2、创建输入流和输出流的对象
            fr = new FileReader(srcFile);
            fw = new FileWriter(destFile);
            //3、数据的读入和写出操作
            char[] cbuf = new char[5];
            int len;
            while ((len = fr.read(cbuf)) != -1){
//              for(int i = 0;i < len;i++){
//                  fw.write(cbuf[i]);
//              }
                //用这个重载的write()方法,比上面更简便一些。
                fw.write(cbuf,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4、关闭输入流资源
            if(fr != null)
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            
            //关闭输出流资源
            if(fw != null)
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }

使用FileInputStream和FileOutputStream实现非文本文件(如.jpg)的复制

package java1;

import org.junit.Test;

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

public class FileInputOutputStreamTest {
    @Test
    public void test(){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            File srcFile = new File("学生证.jpg");
            File destFile = new File("学生证1.jpg");

            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);

            byte[] buffer = new byte[5];
            int len;
            while((len = fis.read(buffer)) != -1){
                fos.write(buffer,0,len);
            }
        } 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();
                }
        }
    }
}

上一篇 下一篇

猜你喜欢

热点阅读