java实现简单的IO字节流读写操作

来源:互联网 发布:犀牛5.0mac 编辑:程序博客网 时间:2024/05/16 17:31
package com.yang.stream;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;public class FileDemo {    public static void main(String[] args) {        String oldpath = "d:/a.txt";        String newpath = "d:/b.txt";        BufferedInputStream in = null;        BufferedOutputStream os = null;        try {            in = new BufferedInputStream(new FileInputStream(new File(oldpath)));            os = new BufferedOutputStream(new FileOutputStream(new File(newpath)));            byte[] b = new byte[1024];            int len = 0;            while((len=in.read(b))!=-1){                os.write(b, 0, len);            }            os.flush();        } catch (FileNotFoundException e) {            System.out.println("文件不存在");        } catch (IOException e) {            System.out.println("输入输出异常");        } finally {            if(os!=null){                try {                    os.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if(in!=null){                try {                    in.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }        System.out.println("文件传输成功");    }}
原创粉丝点击