Java IO流 实现文件复制

Java IO流 实现文件复制

  • 思路

1、将指定文件转换成输入流
2、创建数组接收输入字节
3、将文件复制想要存放的轮径创建输出流
4、将输入流的字节输出出去

  • 实现
	  //创建输入和输出流
      FileInputStream fis = new FileInputStream("D:11.mp4");
      FileOutputStream fos = new FileOutputStream("D:1212.mp4");
      //创建字节接收数组
      byte[] b = new byte[1024];
      //while循环读写字节 当read()函数=-1时读取结束跳出循环
      while (fis.read(b)!=-1){
          fos.write(b);
      }
      //关闭资源
      fis.close();
      fos.close();
  • 以此类推
//复制文件11种方法  
public class Test01 {  
    public static void main(String[] args) throws IOException {  
        // 字节流  
        FileInputStream fis = new FileInputStream("a.txt");  
        FileOutputStream fos = new FileOutputStream("f.txt");  
        // 字节缓冲区流  
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("a.txt"));  
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("f.txt"));  
  
        // 字符流  
        InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"));  
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("f.txt"));  
  
        FileReader fr = new FileReader("a.txt");  
        FileWriter fw = new FileWriter("f.txt");  
        // 字符缓冲区流  
        BufferedReader br = new BufferedReader(new FileReader("a.txt"));  
        BufferedWriter bw = new BufferedWriter(new FileWriter("f.txt"));  
  
        // 字节流方法  
        copyFiles1(fis, fos);  
        copyFiles2(fis, fos);  
  
        copyFiles3(bis, bos);  
        copyFiles4(bis, bos);  
  
        // 字符流读取方法  
        copyFiles5(isr, osw);  
        copyFiles6(isr, osw);  
  
        copyFiles7(fr, fw);  
        copyFiles8(fr, fw);  
  
        // 高效方法  
        copyFiles9(br, bw);  
        copyFiles10(br, bw);  
  
        // 特效方法  
        copyFiles11(br, bw);  
    }  
  
    // 字节读取方法  
    private static void copyFiles1(FileInputStream fis, FileOutputStream fos) throws IOException {  
        // 一次读取一个字节  
        int bys = 0;  
        while ((bys = fis.read()) != -1) {  
            fos.write(bys);  
        }  
        // 关闭流  
        fos.close();  
        fis.close();  
  
    }  
  
    private static void copyFiles2(FileInputStream fis, FileOutputStream fos) throws IOException {  
        // 一次读取一个字节数组  
        byte[] bys = new byte[1024];  
        int len = 0;  
        // 读一个字节数组  
        while ((len = fis.read(bys)) != -1) {  
            fos.write(bys, 0, len);  
        }  
        // 关闭流  
        fos.close();  
        fis.close();  
  
    }  
  
    private static void copyFiles3(BufferedInputStream bis, BufferedOutputStream bos) throws IOException {  
        // 一次读取一个字节  
        int bys = 0;  
        while ((bys = bis.read()) != -1) {  
            bos.write(bys);  
        }  
    }  
  
    private static void copyFiles4(BufferedInputStream bis, BufferedOutputStream bos) throws IOException {  
        // 一次读取一个字节数组  
        byte[] bys = new byte[1024];  
        int len = 0;  
        while ((len = bis.read(bys)) != -1) {  
            bos.write(bys, 0, len);  
            bos.flush();  
        }  
        // 关闭流  
        bos.close();  
        bis.close();  
  
    }  
  
    // 字符读取方法  
    // 普通方法  
    // 方式1  
    public static void copyFiles5(InputStreamReader isr, OutputStreamWriter osw) throws IOException {  
        // 一次读取一个字符  
        // InputStreamReader是FileReader的父类  
        int ch = 0;  
        while ((ch = isr.read()) != -1) {  
            osw.write(ch);  
            osw.flush();  
        }  
  
        isr.close();  
        osw.close();  
    }  
  
    // 方式2  
    public static void copyFiles6(InputStreamReader isr, OutputStreamWriter osw) throws IOException {  
        // 一次读取一个字符数组  
        char[] ch = new char[1024];  
        int len = 0;  
        while ((len = isr.read(ch)) != -1) {  
            osw.write(ch, 0, len);  
            osw.flush();  
        }  
  
        isr.close();  
        osw.close();  
    }  
  
    // 方法3  
    public static void copyFiles7(FileReader fr, FileWriter fw) throws IOException {  
        // 普通方法一次读写一个字符  
        int ch = 0;  
        while ((ch = fr.read()) != -1) {  
            fw.write(ch);  
            fw.flush();  
        }  
  
        fw.close();  
        fr.close();  
    }  
  
    // 方法4  
    public static void copyFiles8(FileReader fr, FileWriter fw) throws IOException {  
  
        // 普通方法一次读取一个字符数组  
        char[] chs = new char[1024];  
        int len = 0;  
        while ((len = fr.read(chs)) != -1) {  
            fw.write(chs, 0, len);  
            fw.flush();  
        }  
  
        fw.close();  
        fr.close();  
    }  
  
    // 高效方法  
    // 方法5  
    public static void copyFiles9(BufferedReader br, BufferedWriter bw) throws IOException {  
        // 高效一次读取一个字符  
        int ch = 0;  
        while ((ch = br.read()) != -1) {  
            bw.write(ch);  
            bw.flush();  
        }  
  
        br.close();  
        bw.close();  
    }  
  
    // 方法6  
    public static void copyFiles10(BufferedReader br, BufferedWriter bw) throws IOException {  
        // 高效一次读取一个字符数组  
        char[] chs = new char[1024];  
        int len = 0;  
        while ((len = br.read(chs)) != -1) {  
            bw.write(chs, 0, len);  
            bw.flush();  
        }  
  
        br.close();  
        bw.close();  
    }  
  
    // 特效方法  
    // 方法7  
    public static void copyFiles11(BufferedReader br, BufferedWriter bw) throws IOException {  
        // 特效方式  
        String string = null;  
        // 一次读写一行  
        while ((string = br.readLine()) != null) {  
            bw.write(string);  
            bw.newLine();  
            bw.flush();  
        }  
        // 关闭输入输出流  
        br.close();  
        bw.close();  
    }  
  
}  

借鉴出处

你可能感兴趣的:(IO流,java,开发语言)