字节流,字符流文件复制总结,包含9种方法,都是标准写法

package cn.itcast_03;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Demo03 {

public static void main(String[] args) {
        multi_byte_excute("123.mp4", "332.mp4");
    }

// 字节流操作
    // 单字节文件复制(FileInputStreams,FileOutputStreams),适合文件的操作,即不用拿来看的,速度快,因为没有编码的操作,纯粹字节码
    public static void single_byte_excute(String infile, String outfile) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(infile);
            fos = new FileOutputStream(outfile);
            int by = 0;
            while ((by = fis.read()) != -1) {
                fos.write(by);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException 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();
                }
            }
        }

}

// 字节数组文件复制(FileInputStreams,FileOutputStreams),适合文件的操作,即不用拿来看的,速度快,因为没有编码的操作,纯粹字节码
    public static void multi_byte_excute(String infile, String outfile) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(infile);
            fos = new FileOutputStream(outfile);
            int len = 0;
            byte[] bys = new byte[1024];
            while ((len = fis.read(bys, 0, bys.length)) != -1) {
                fos.write(bys, 0, len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException 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();
                }
            }
        }
    }

// 高效单字节文件复制(BufferedInputStreams,BufferedOutputStreams),增加了缓冲区,减少多次IO操作,IO操作消耗很大,有缓冲区能够增加效率,适合文件的操作,即不用拿来看的,速度快,因为没有编码的操作,纯粹字节码
    public static void buffer_single_byte_excute(String infile, String outfile) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(infile));
            bos = new BufferedOutputStream(new FileOutputStream(outfile));
            int by = 0;
            while ((by = bis.read()) != -1) {
                bos.write(by);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

// 高效字节数组文件复制(BufferedInputStreams,BufferedOutputStreams),增加了缓冲区,减少多次IO操作,IO操作消耗很大,有缓冲区能够增加效率,适合文件的操作,即不用拿来看的,速度快,因为没有编码的操作,纯粹字节码
    public static void buffer_multi_byte_excute(String infile, String outfile) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(infile));
            bos = new BufferedOutputStream(new FileOutputStream(outfile));
            int len = 0;
            byte[] array = new byte[1024];
            while ((len = bis.read(array, 0, array.length)) != -1) {
                bos.write(array, 0, len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

// ------------------------------------------------------------------------
    // 字符流操作
    // 单个字符操作(OutputStreamWriter,称为字符流转字节流,InputStreamReader,称为字节流转字符流)适合文本文件
    public static void sinle_char_excute(String infile, String outfile) {
        OutputStreamWriter osw = null;
        InputStreamReader isr = null;

try {
            osw = new OutputStreamWriter(new FileOutputStream(outfile));
            isr = new InputStreamReader(new FileInputStream(infile));
            int ch = 0;
            while ((ch = isr.read()) != -1) {
                osw.write(ch);
                osw.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (osw != null) {
                try {
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (isr != null) {
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

// 字符数组操作(OutputStreamWriter,称为字符流转字节流,InputStreamReader,称为字节流转字符流)适合文本文
    public static void multi_char_excute(String infile, String outfile) {
        OutputStreamWriter osw = null;
        InputStreamReader isr = null;
        try {
            osw = new OutputStreamWriter(new FileOutputStream(outfile));
            isr = new InputStreamReader(new FileInputStream(infile));
            int len = 0;
            char[] chs = new char[1024];
            while ((len = isr.read(chs, 0, chs.length)) != -1) {
                osw.write(chs, 0, len);
                osw.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (osw != null) {
                try {
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (isr != null) {
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

// 简便字符操作包装类,单字符输入输出复制(FileWriter,FileReader)
    public static void simple_single_char_excute(String infile, String outfile) {
        FileWriter fw = null;
        FileReader fr = null;
        try {
            fw = new FileWriter(outfile);
            fr = new FileReader(infile);
            int ch = 0;
            while ((ch = fr.read()) != -1) {
                fw.write(ch);
                fw.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException 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();
                }
            }
        }
    }

// 简便字符操作包装类,字符数组输入输出复制(FileWriter,FileReader)
    public static void simple_multi_char_excute(String infile, String outfile) {
        FileWriter fw = null;
        FileReader fr = null;
        try {
            fw = new FileWriter(outfile);
            fr = new FileReader(infile);
            int len = 0;
            char[] chs = new char[1024];
            while ((len = fr.read(chs, 0, chs.length)) != -1) {
                fw.write(chs, 0, len);
                fw.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException 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();
                }
            }
        }
    }

// 高效单字符缓冲流复制操作
    public static void buffer_single_char_excute(String infile, String outfile) {
        BufferedWriter bw = null;
        BufferedReader br = null;
        try {
            // bw = new BufferedWriter(new OutputStreamWriter(new
            // FileOutputStream(outfile)));
            bw = new BufferedWriter(new FileWriter(outfile));
            // br = new BufferedReader(new InputStreamReader(new
            // FileInputStream(infile)));
            br = new BufferedReader(new FileReader(infile));

int ch = 0;
            while ((ch = br.read()) != -1) {
                bw.write(ch);
                bw.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

// 高效字符数组缓冲流复制操作
    public static void buffer_multi_char_excute(String infile, String outfile) {
        BufferedWriter bw = null;
        BufferedReader br = null;
        try {
            // bw = new BufferedWriter(new OutputStreamWriter(new
            // FileOutputStream(outfile)));
            bw = new BufferedWriter(new FileWriter(outfile));
            // br = new BufferedReader(new InputStreamReader(new
            // FileInputStream(infile)));
            br = new BufferedReader(new FileReader(infile));
            int len = 0;
            char[] chs = new char[1024];
            while ((len = br.read(chs, 0, chs.length)) != -1) {
                bw.write(chs, 0, len);
                bw.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

// 高效缓冲字符流特有功能进行文本文件复制操作,读行写行操作
    public static void buffer_special_char_file_copy(String infile, String outfile) {
        BufferedWriter bw = null;
        BufferedReader br = null;
        try {
            bw = new BufferedWriter(new FileWriter(outfile));
            br = new BufferedReader(new FileReader(infile));
            String str = null;
            //默认br.readLine()只读该行内容,不会读取该行的换行符
            while ((str = br.readLine()) != null) {
                bw.write(str);
                bw.newLine();
                bw.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

}
}

时间: 2024-08-08 22:17:28

字节流,字符流文件复制总结,包含9种方法,都是标准写法的相关文章

java学习笔记之字符流文件复制

字符文件复制 1 FileReader fr =new FileReader("b.txt");//绑定源文件 2 FileWriter fw= new FileWriter("a.txt"); 3 char[] c= new char[1024]; 4 int len=0; 5 //循环读取 6 while((len=fr.read(c))!=-1){ 7 fw.write(c,0,len); 8 9 } 10 //关流 11 fw.close(); 12 fr.

IO流,字节流复制文件,字符流+缓冲复制文件

JAVAIO如果按流向分:输入流和输出流两种 输入流的基类:InputStream   Reader 输出流的基类:OutputStream   Writer 如果按数据单元划分:字节流和字符流 字节流输入输出的基类:InputStream  OutputStream 字符流输入输出的基类:Reader   Writer 字节流复制文件内容 public static void main(String[] args) { //字节流复制文件内容 InputStream io=null; Outp

IO-03字节流字符流

在程序中所有的数据都是以流的方式进行传输和保存的,程序需要数据的时候要使用输入流读取数据,而程序需要将一些数据保存起来则需要使用输出流来完成. 在JAVA.IO包中操作文件内容的主要有两大类:字节流,字符流,两类都分为输入和输出操作.在字节流中输出数据主要是使用OutputStream,输入主要使用InputStream,在字符流中输出主要使用Writer类完成,输入只要使用Reader类完成. 主要的操作流程如下: 使用FILE类打开一个文件 通过字节流或者字符流的子类,指定输出的位置. 进行

Android笔记:利用InputStream和BufferedReader 进行字节流 字符流处理

通过socket获取字节流处理时最初使用的是BufferedReader和PrintWriter  这种方式在解析字符串时是比较方便的 但是在处理字节时不够方便最终还是回归到InputStream和OutputStream方式 不使用缓存直接使用字节流操作,一次传输的字节数据在300字节以内,目前没有测试差距会有多大. import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException;

PHP中获取文件扩展名的N种方法

PHP中获取文件扩展名的N种方法 从网上收罗的,基本上就以下这几种方式: 第1种方法: function get_extension($file) { substr(strrchr($file, '.'), 1); } 第2种方法: function get_extension($file) { return substr($file, strrpos($file, '.')+1); } 第3种方法: function get_extension($file) { return end(expl

PHP中 获取文件扩展名的N种方法

PHP中获取文件扩展名的N种方法,有以下这几种方式:第1种方法:function get_extension($file){substr(strrchr($file, ‘.’), 1);} 第2种方法:function get_extension($file){return substr($file, strrpos($file, ‘.’)+1);} 第3种方法:function get_extension($file){return end(explode(‘.’, $file));} 第4种

Mac 中显示资源库(Library)文件夹目录的几种方法

Mac 中显示资源库(Library)文件夹目录的几种方法 Mac中Library目录在10.6.7系统之后默认隐藏的,要想找到此文件夹有如下几种方法: 1. 用命令可以使其显示: 在终端中执行命令: chflags nohidden ~/Library 可显示资源库文件夹 如想隐藏,可以在终端中执行命令: chflags hidden ~/Library 隐藏 2. 在Finder菜单中的偏好设置中设置 在Finder菜单中的偏好设置中选择边栏,勾选上设备中的硬盘. 再打开Finder,Fin

Java读取文件夹大小的6种方法及代码

这篇文章介绍了JAVA读取文件夹大小的几种方法实例,有需要的朋友可以参考一下. (一)单线程递归方式 package com.taobao.test; import java.io.File; public class TotalFileSizeSequential { public static String fileName = "C:\\Documents and Settings\\Administrator\\桌面\\monkeytalk"; // 递归方式 计算文件的大小

IO 复习字节流字符流拷贝文件

/* 本地文件 URL 文件拷贝 *//*文本文件拷贝 可以通过 字符流,也可以通过字节流*/ /*二进制文件拷贝 只可以通过字节流*//* 希望这个例子能帮助搞懂 字符流与字节流的区别 */ import java.io.*; //in order to utilize stream object import java.util.*; // in order to utilize ArrayList import java.net.*; class Copy{ public static v