【Java笔记】IO流-节点流(FileReader ( )与FileWriter( ))文本文件读写和(FileInputStream ( ) 与 FileOutputStream )非文本文件读取

I / O 是 Input / Output 的缩写,I / O 技术是非常实用的技术,用于处理设备之间的数据传输。如读写文件,网络通讯等

Java 程序中,对于数据的输入输出操作以 “流” 的方式进行。java . io 包下提供了各种 “流” 类和接口,用以不同种类的数据,并通过标准的方法输入或输出数据

目录

Java IO原理

流的分类

抽象基类

字符流

FileReader ( )

FileWriter ( )

FileReader ( )与FileWriter ( )实现文本文件的复制

字节流

FileInputStream ( ) 与 FileOutputStream ( ) 实现对图片的复制操作


Java IO原理

输入 input:读取外部数据(磁盘、光盘等存储设备的数据)到程序(内存)中

输出 output:将程序(内存)数据输出到磁盘、光盘等存储设备中

流的分类

按操作数据单位不同分为:字节流(8 bit),字符流(16 bit)

按数据流的流向不同分为:输入流,输出流

按流的角色不同分为:节点流(文件流),处理流

        

抽象基类

对于文本文件(.txt, .java, .c, .cpp)使用字节流处理

对于非文本文件(.jpg, .mp3, .mp4, .avi, .doc, .ppt, ...)使用字符流处理

异常处理:为了保证流的资源一定可以执行关闭操作。需要执行 try-catch-finally 处理

字符流

FileReader ( )

读取外部数据到内存中

read()的理解:返回读入的第一个字符。如果达到文件末尾,返回 -1

import org.junit.Test;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class IOTest {
    @Test
    public void testFileReader(){
        FileReader fr = null;
        try {
            //1. 实例化 File 类的对象,指明要操作的文件
            File file = new File("D:\\IDEACode\\DailyTest\\src\\IOTest\\hello.txt");//相较于当前 Module
            //2. 提供具体的流
            fr = new FileReader(file);
            //3. 数据读入
            //read():返回读入的一个字符。如果达到文件末尾,返回 -1
//            int data = fr.read();
//            while ((data = fr.read()) != -1){
//                System.out.print((char)data);
//            }
            //read(char[] cbuf):返回每次读入 cbuf 数组中的字符的个数
            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]);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4. 流的关闭操作
            try {
                if (fr != null)
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

>>> hello

FileWriter ( )

从内存中写入数据到硬盘文件里

1. 输出操作:对应的 File 可以不存在,不会报异常

2. File 对应的硬盘中的文件如果不存在,在输出的过程中,会自动创建此文件

    File 对应的硬盘中的文件如果存在:

                如果流使用的构造器是:FileWriter (flie, false) / FileWriter (file):对原有文件的覆盖

                如果流使用的构造器是:FileWriter (flie, true):不会对原有文件覆盖,而是在原有文件                        基础上追加内容

import org.junit.Test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterTest {
    @Test
    public void testFileWriter() {
        FileWriter fw = null;
        try {
            //1. 提供 File 类的对象,指明写出到的文件
            File file = new File("hello1.txt");
            //2. 提供 FileWriter 对象,用于数据写出
            fw = new FileWriter(file);
            //3. 写出的操作
            fw.write("Hello Java");
            //4. 流的关闭
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fw != null)
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

FileReader ( )与FileWriter ( )实现文本文件的复制

import org.junit.Test;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class testFileReaderFileWriter {
    @Test
    public void testFileReaderFileWriter(){
        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;//记录每次读入 cbuf 数组中的字符个数
            while ((len = fr.read(cbuf)) != -1){
                fw.write(cbuf,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //4. 关闭资源流
                if (fw != null)
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

字节流

字符流的基本操作与字节流相同,这里我们直接上综合代码

FileInputStream ( ) 与 FileOutputStream ( ) 实现对图片的复制操作

import org.junit.Test;
import java.io.*;

public class testFileInputOutputStream {
    @Test
    public void testFileInputOutputStream(){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //1. 创建 File 类的对象,指明读入和写入的图片
            File srcFile = new File("saber.jpg");
            File destFile = new File("saber1.jpg");
            //2. 创建输入流和输出流的对象
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);
            //3. 图片的复制
            byte[] buffer = new byte[5];
            int len;
            while ((len = fis.read(buffer)) != -1){
                fos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4. 关闭资源流
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java小白。。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值