Java自学笔记——IO流

本文档详细介绍了Java中的IO流概念,包括字节流和字符流的区别,以及FileInputStream和FileOutputStream的实战应用。通过 FileInputStream读取文件,展示了read()和read(byte[])方法的使用,并讲解了FileOutputStream如何进行文件写入,包括追加和覆盖。最后,展示了文件拷贝的完整流程,涉及流的打开、读取和关闭。

Java自学笔记——IO流

分类

  • 文件流
    流:数据在数据源(文件)和程序(内存)之间经历的路径
    输入输出流(以程序为主体

文件与IO流的区别
请添加图片描述

分类

字节流 8bit InputStream OutputStream 字节文件如视频图片等
字符流 按字符 Reader Writer 文本文件
都是抽象类,不能实例化

结点流 处理流(包装流)
##FileInputStream

package com.hz.file.FileInputStream;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author Lspero
 * @version 1.0
 * 字节输入流,文件->程序
 *
 

public class FileInputStream_ {
    public static void main(String[] args) {

    }

    /*
    read()单个字节读取,效率低
     */
    @Test
    public void readFile01(){
        String filePath = "c:\\D\\hello.txt";
        int readData = 0;
        FileInputStream fileInputStream = null;
        try {
            //创建FileInputStream文件,用于读取文件
            fileInputStream = new FileInputStream(filePath);
            //如果返回-1,就到达文件尾
            while ((readData = fileInputStream.read()) != -1){
                System.out.print((char)readData);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭流,释放文件
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    //read(Byte[] b
    @Test
    public void readFile02(){
        String filePath = "c:\\D\\hello.txt";
        int readLen = 0;
        byte[] buf = new byte[8];//一次读8个字节
        FileInputStream fileInputStream = null;
        try {
            //创建FileInputStream文件,用于读取文件
            fileInputStream = new FileInputStream(filePath);
            //如果读取正常,返回实际读取的字节数,如果返回-1,就到达文件尾
            while ((readLen = fileInputStream.read(buf)) != -1){
                System.out.print(new String(buf, 0, readLen));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭流,释放文件
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

FileOutputStream

package com.hz.file;

import org.junit.Test;

import java.io.*;
import java.io.FileOutputStream;

/**
 * @author Lspero
 * @version 1.0
 */
public class FileOutputStream01 {
    public static void main(String[] args) {

    }

    /*
    使用FileOutputStream将数据写到文件中
    如果不存在,就创建
     */
    @Test
    public void writeFile(){
        String filePath = "c:\\D\\a.txt";
        FileOutputStream fileOutputStream = null;
        

        try {
            // new FileOutputStream(filePath)创建方式,写入内容时是覆盖
            //new FileOutputStream(filePath,true)追加到后面
            fileOutputStream = new FileOutputStream(filePath,true);
            

            //写入一个字节
            fileOutputStream.write('H');
            //写入字符串
            String s = "Hello world!";
            //getBytes()字符串转为字节数组
            fileOutputStream.write(s.getBytes());
            //从off开始写len个
            fileOutputStream.write(s.getBytes(),0,2);
            

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

}

拷贝的完成

package com.hz.file;

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

/**
 * @author Lspero
 * @version 1.0
 */
public class FileCopy {
    public static void main(String[] args) {
        /*完成文件拷贝a.txt拷贝到A目录下
            1.创建输入流,读入程序
            2.创建输出流,写入磁盘
            执行行,读取部分数据就写入
        */

        String srcFilePath = "c:\\D\\a.txt";
        String destFilePath = "c:\\D\\A\\a.txt";
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(srcFilePath);
            fileOutputStream = new FileOutputStream(destFilePath);
            //定义数组
            byte[] buf = new byte[1024];
            int readLen = 0;
            while ((readLen = fileInputStream.read(buf)) != -1){
                fileOutputStream.write(buf,0,readLen);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭流
            if(fileInputStream != null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fileOutputStream != null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值