美文网首页
IO流之节点流(文件流)

IO流之节点流(文件流)

作者: 冯宏志 | 来源:发表于2020-12-30 17:19 被阅读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()用法
     //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();
                }
        }
    }
}

相关文章

  • IO流之节点流(文件流)

    IO流的分类 按操作的数据单位:字节流(8bit),字符流(16bit)---->对于文本文件使用字符流处理,对于...

  • JAVA API-day07

    A 基本的IO操作 文件流 缓冲流 节点流和处理流 对象流

  • 13. IO流

    一、文件创建文件: 二、IO流分类 三、节点流FileInputStream文件字节输入流: 四、节点流FileO...

  • IO

    IO流 创建源 -> 选择流 -> 操作 -> 释放 节点流 1.2.3.4. 1.访问文件 FileInputS...

  • IO流——节点流

    定义 输入Input:读取外部数据(磁盘,光盘等存储设备的数据)到程序(内存)中。 输出output:将程序(内存...

  • Java IO之节点流(文件流)的使用

    文件字符流FileReader和FileWriter的使用 文件的输入 从文件中读取到内存(程序)中 步骤: 建立...

  • java基础-day20-IO流和StringBuffer

    IO流和StringBuffer 1. IO流 1.1 IO流概述 1.2 IO流分类 1.3 文件操作输入输出字...

  • Java IO 节点流和字符流 学习 + 案例分析

    Java IO 节点流和字符流 学习 + 案例分析

  • IO流

    IO 输入是读取,输出是写出,流是单向的 节点流,也叫低级流 节点流有明确的来源与去向 处理流,也叫高级流 处理流...

  • 5-Java IO-装饰模式

    1.Java IO分类节点流-过滤流输入流-输出流字节流-字符流 2.InputStream,节点输入字节流的祖宗...

网友评论

      本文标题:IO流之节点流(文件流)

      本文链接:https://www.haomeiwen.com/subject/eevegktx.html