IO流第34天(文件拷贝(同时读取和写入文件),FileReader()读取文件)

文件拷贝

完成文件拷贝,将 d:\风景.jpg 图片拷贝 到c:\

    //完成文件拷贝,将 d:\\风景.jpg 图片拷贝 到c:\\
    //思路
    //1,创建文件输入流,将文件读入到程序
    //2,创建文件的输出流,将读取到的文件数据写入到指定的
    String srcFilePath="d:\\风景.jpg";
    String destFilePath="c:\\风景21.jpg";
    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写入,即边读边写
            fileOutputStream.write(buf,0,readLen);//一定要使用此方法
        }
        System.out.println("拷贝成功");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            //关闭输入流和输出流,释放资源
            if(fileInputStream!=null)
            {
                fileInputStream.close();
            }
            if(fileOutputStream!=null)
            {
                fileOutputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

注意:
1.边读边写,写的时候用FileOutputStream(数组,起始位置,结束位置)
2.Finally同时关闭输入流和输出流
3.拷贝到c盘时可能会权限不够导致失败
4.如果文件已存在,再拷贝也不会报错

FileReader和FileWriter

FileReader和FileWriter是字符流,即按照字符来操作io

FileReader

FileReader相关方法:

1)new FileReader(File/String)
2)read():每次读取单个字符,返回该字符,如果到文件末尾则返回-1
3)read(char[]):批量读取多个字符到数组,返回读取到的字符数,如果到文件末尾则返回-1

相关API:

new String(char[]):将char[]转换成String
new String(char[],off,len):将char[]的指定部分转换成String

image
案例:
使用FileReader从story.txt读取内容并显示
使用read()读取单个字符

@Test
    public void readFile01()
    {
        //创建FileRead对象
        FileReader fileReader=null;
        String filePath="d:\\story.txt";
        int date=0;
        try {
            fileReader=new FileReader(filePath);
            while ((date=fileReader.read()) != -1)
            {
                System.out.print((char)date);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(fileReader!=null)
                {
                    fileReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

使用read(buf)读取数组

@Test
    public void readFile02(){
        //创建FileRead对象
        FileReader fileReader=null;
        String filePath="d:\\story.txt";
        char []buf=new char[8];
        int readLen=0;
        try {
            fileReader=new FileReader(filePath);
            while ((readLen=fileReader.read(buf)) != -1)
            {
                System.out.print(new String(buf,0,readLen));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(fileReader!=null)
                {
                    fileReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

注意:乱码的话需要修改编码方式

FileWriter

FileW****rite常用方法

1)new FileWrite(File/String)//覆盖模式
2)new FileWrite(File/String,true)//追加模式
3)write(int)//写入单个字符
4)write(char[])//写入指定数组
5)write(char[],off,len)//写入指定数组的指定部分
6)write(String)//写入整个字符串
7)write(String,off,len)//写入字符串的指定部分

注意:FileWrite使用后,必须要关闭(close)或刷新(flush),否则写入不到指定的文件
image

posted @   不再犹豫27  阅读(82)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示