关闭

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

标签: 文件拷贝字节字符FileReaderFileInputStream
226人阅读 评论(0) 收藏 举报
分类:

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

两者区别:字节流无缓冲区,字符流有缓冲区,必须关闭流(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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:109856次
    • 积分:2223
    • 等级:
    • 排名:第17655名
    • 原创:76篇
    • 转载:63篇
    • 译文:2篇
    • 评论:12条
    最新评论