IO流笔记

在这里插入图片描述
在这里插入图片描述

文件基础知识

在这里插入图片描述
在这里插入图片描述

创建文件

在这里插入图片描述

package org.example;
import org.junit.Test;

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

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

    }
    @Test
    //方式1:new File(Strin pathname)
    public void create01(){
        File file = new File("d:\\news.txt");
        try{
            file.createNewFile();
            System.out.println("创建成功");
        } catch (IOException e){
            e.printStackTrace();
        }
    }
    @Test
    //方式2:new File(File parent,Stirng chile)//根据父目录文件+子路径构成
    public void create02(){
        File parentFile=new File("d:\\");
        String fileName="news2.txt";
        //在内存中创造了一个File对象
        File file = new File(parentFile,fileName);
        try{
            //将文件真正的创建到硬盘上
            file.createNewFile();
            System.out.println("创建成功");
        } catch (IOException e){
            e.printStackTrace();
        }
    }
    @Test
    //方式3:new File(String parent,Stirng child)//根据父目录+子路径构建
    public void create03(){
        String parentPath ="d:\\";
        String fileName = "news3.txt";
        //在内存中创造了一个File对象
        File file = new File(parentPath,fileName);
        try{
            //将文件真正的创建到硬盘上
            file.createNewFile();
            System.out.println("创建成功");
        } catch (IOException e){
            e.printStackTrace();
        }
    }
}

output:
在这里插入图片描述

文件操作

package org.example;

import org.junit.Test;

import java.io.File;
import java.sql.SQLOutput;

public class FileInformation {
    public static void main(String[] args) {
        
    }
    @Test
    //获取文件的信息
    public void info(){
        //先创建文件对象
        File file=new File("d:\\news.txt");
        //文件名
        System.out.println("文件名"+file.getName());
        System.out.println("绝对路径"+file.getAbsoluteFile());
        System.out.println("父级目录"+file.getParent());
        System.out.println("文件大小(字节)"+file.length());
        System.out.println("文件是否存在"+file.exists());
        System.out.println("是不是一个文件"+file.isFile());
        System.out.println("是不是一个目录"+file.isDirectory());

    }
}

output:
在这里插入图片描述

目录的创建 及 目录与文件的删除

在这里插入图片描述
注意:是删除空目录,文件是不是空的都能删除。

package org.example;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.sql.SQLOutput;

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

    }
    @Test
    public void m1(){
        String filePath="d:\\news.txt";
        File file=new File(filePath);
        if(file.exists()){
            if(file.delete()){
                System.out.println("删除成功");
            }else{
                System.out.println("删除失败");
            }
        }else{
            System.out.println("文件不存在");
        }
    }
    @Test
    //Java中目录也被看作文件
    public void m2(){
        String directoryPath="d:\\demo02";
        File file=new File(directoryPath);
        if(file.exists()){
            if(file.delete()){
                System.out.println("删除成功");
            }else{
                System.out.println("删除失败");
            }
        }else{
            System.out.println("文件不存在");
        }
    }
    @Test
    ///判断 D:\\demo\\a\\b\\c 目录是否存在,如果存在就提示已经存在,否则就创建
    public void m3(){
        String directoryPath="d:\\demo\\a\\b\\c";
        File file=new File(directoryPath);
        if(file.exists()){
            System.out.println("目录存在");
        }else{
            if(file.mkdirs()){
                System.out.println("该目录创建成功");
            }else{
                System.out.println("创建失败");
            }
        }
    }
}

IO流原理和分类

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
上图表中的四个类都是抽象类,不能直接使用
在这里插入图片描述

FileInputStream

在这里插入图片描述
在这里插入图片描述

package org.example.InputStream;
import org.junit.Test;

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

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

    }

    /**
     * 演示读取文件
     * 单个字符的读取,效率比较低
     * —————>使用read(byte[] b)优化
     */
    @Test
    public void readFile01(){
        String filePath="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);//转成char
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    /**
     * 使用read(byte[] b)读取文件提高效率
     * 
     * 在下面的代码中,buf数组的作用是用于存储从文件中读取到的字节数据。在读取文件内容的过程中,
     * 通过fileInputStream.read(buf)方法将读取到的字节数据存储在buf数组中。buf数组的大小为8个字节,即byte[8],
     * 一次最多读取8个字节的数据。然后,通过new String(buf, 0, readLen)将buf数组中的字节数据转换为字符串,并打印出来。
     * 因为读取文件的效率通常比较高,所以预先申请一个较小的字节数组,每次读取一部分数据到该数组中,可以提高读取效率。
     * buf数组的大小可以根据实际需要进行调整,以适应不同的文件大小和性能要求。
     */
    @Test
    public void readFile02(){
        String filePath="d:\\hello.txt";
        byte[] buf=new byte[8];//一次读取8个字节
        int readLen=0;
        FileInputStream fileInputStream = null;
        {
            try {
                //创建FileInputStream对象,用于读取文件
                fileInputStream = new FileInputStream(filePath);

                while((readLen=fileInputStream.read(buf))!=-1){
                    System.out.print(new String(buf,0,readLen));//显示
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

}

FileOutputStream

在这里插入图片描述

package org.example.OutputStream;
import org.junit.Test;

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

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

    }
    /**
     * 演示FileOutputStream,将数据写到文件
     * 如果文件不存在则创建文件
     */
    @Test
    public void wFile() {
        //创建FileOutputStream
        String filePath="d:\\a.txt";
        FileOutputStream fileOutputStream=null;
        try {
            //得到FileOutputStream对象
            //1、new FileOutputStream(filePath)这种创建方式是会覆盖原来的内容
            //2、new FileOutputStream(filePath,true)这种创建方式会追加
            fileOutputStream=new FileOutputStream(filePath,true);
            //写入一个字节
            //fileOutputStream.write('!');
            //写入一个字符串
            String str="Hello World";
            //getBytes可以将字符串转化为一个字符数组
            //fileOutputStream.write(str.getBytes());
            //write(byte[] b, int off,int len)输出从off开始的len的字符
            fileOutputStream.write(str.getBytes(),0,3);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }

}

文件拷贝

package org.example.OutputStream;

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

public class FileCopy {
    public static void main(String[] args) {
        //完成文件拷贝,将d:\\kkk.jpg 拷贝到c:\\
        //思路分析:
        //1、创建文件的输入流,将文件读入程序
        //2、创建文件的输出流,将读取到的文件数据写入指定的文件
        String srcfilePath="d:\\kkk.jpg";
        String destFilePath ="d:\\kpp.jpg";
        FileInputStream fileInputStream=null;
        FileOutputStream fileOutputStream=null;

        try{
            fileInputStream =new FileInputStream(srcfilePath);
            fileOutputStream=new FileOutputStream(destFilePath);
            byte[] buff=new byte[1024];
            //readLen是一个整型变量,用于保存每次从源文件中读取的字节数量。
            //在代码中,通过`fileInputStream.read(buff)`方法读取源文件中的内容,并将读取到的字节数量赋值给`readLen`。
            // 然后在后续的逻辑中,根据`readLen`的值来判断是否已经达到文件末尾。如果`readLen`的值为-1,表示已经读取到文件末尾;否则,表示仍有字节可读。
            int readLen=0;
            while((readLen=fileInputStream.read(buff))!=-1){
                //边读边写
                fileOutputStream.write(buff,0,readLen);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try{
                if(fileOutputStream!=null){
                    fileOutputStream.close();
                }
                if(fileInputStream!=null){
                    fileInputStream.close();
                }
            } catch (IOException E){
                E.printStackTrace();
            }
        }
    }
}

字符流

在这里插入图片描述

在这里插入图片描述

FileReader

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值