字符拷贝和字节拷贝文件对比(FileInputStream,FileOutputStream, FileReader, FileWriter)

拷贝文件的思想就是从源文件路径读取文件,然后往目标文件路径写入文件

两者区别:字节流无缓冲区,字符流有缓冲区,必须关闭流(close操作)或者清空缓冲区(flush操作)才能写入成功

1:按字节拷贝文件( FileInputStream,FileOutputStream

import java.io.FileInputStream;
public class FileCopy
{
    public static void main(String[] args)
    {
        
        // 将  A 文件  拷贝到 B 文件
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try
        {
            fis = new FileInputStream("C:\\Users\\User02\\Desktop\\io.png");
            fos = new FileOutputStream("C:\\Users\\User02\\Desktop\\io.txt",true);
            // 默认会覆盖原有的数据,如果想追加append
            byte[] b = new byte[10];
            int i = 0;
            while((i = fis.read(b)) != -1)
            {
                fos.write(b,0,i);
            }
            
        }
        catch (Exception 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();
                }
            }
            
        }
        
    }
}


2:按字符拷贝文件(FileReader, FileWriter


import java.io.FileInputStream;

public class TestFileReaderAndWriter
{
    public static void main(String[] args)
    {
        
        // 将  A 文件  拷贝到 B 文件
        FileReader fr = null;
        FileWriter fw = null;
        try
        {
            fr = new FileReader("src/com/itany/coreJava/day18/a.txt");
            fw = new FileWriter("src/com/itany/coreJava/day18/b.txt");
            // 默认会覆盖原有的数据,如果想追加append
            

            System.out.println(fr.getEncoding());
            char[] b = new char[10];
            int i = 0;
            while((i = fr.read(b)) != -1)
            {
                fw.write(b,0,i);
            }
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }finally{
            if(fw != null)
            {
                try
                {
                    fw.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            
            if(fr != null)
            {
                try
                {
                    fr.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            
        }
        
    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值