通过IO流讲一个文件里面的内容读到另外一个文件里面(文件复制功能的实现)

来源:互联网 发布:js selected选中事件 编辑:程序博客网 时间:2024/05/21 21:37
package com.test.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class TestInputStream {

    public static void main(String[] args) throws IOException {
        int BUFFER_SIZE = 16 * 1024;
        int b = 0;
        File src = new File("E:\\xx.txt");
        File dst = new File("E:\\xxx.txt");
        InputStream in = null;
        OutputStream out = null;
        try {
            //获得输入流
            in = new BufferedInputStream(new FileInputStream(src));
            
            //创建一个新的缓冲输出流,以将数据写入指定的基础输出流
            out = new BufferedOutputStream(new FileOutputStream(dst));
            //缓存字符数组
            byte[] buffer = new byte[BUFFER_SIZE];
            
            while ((in.read(buffer))!=-1) {
                //将buffer.length 长度的字符写入到输出流里面
                out.write(buffer);
                
            }
        } catch (FileNotFoundException e) {
            System.out.println("系统找不到指定文件");
            System.exit(-1);
        
        }finally  {
            if ( null != in)  {
                in.close();
            }
              if ( null != out)  {
                out.close();
            }
        }
     }

    }


原创粉丝点击