字节输入输出流

//字节流
//InputStream和OutPutStream 读写
public class IOStream {
    //字节数组缓冲读文件
    public static void input(String fileName){
        String s=null;
        FileInputStream fis=null;
        try {
            fis=new FileInputStream(fileName);
            byte[] buffer=new byte[1024];
            int len=-1;
            while((len=fis.read(buffer, 0, buffer.length))!=-1){
                //此处n即为每次读取到的字符个数,当读取为空是是-1
                //三个参数分别是:缓存数组   开始位置   每次存到缓存数组的长度
                //-**********************************
                //String 的一个构造方法,参数是(字节数组 ,开始下标位置,长度)
                //将字节数组转换成一个字符串
                s=new String(buffer, 0, len);
            }
            System.out.println(s);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                if(fis!=null){
                    fis.close();                    
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    //字节数组缓冲写文件
    public static void ouput(String fileName,String s){
        byte[] buffer=s.getBytes();//将字符串转化成字节数组
        FileOutputStream fos = null;
        try {
            fos=new FileOutputStream(fileName);
            fos.write(buffer);//把字节数组写入文件
            fos.flush();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                if(fos!=null){                  
                    fos.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    //字节数组复制文件(边读边写)
    public static void IOput(String sFileName,String dFileName){
        FileInputStream fis=null;
        FileOutputStream fos=null;
        byte[] buffer=new byte[1024];

        try {
            fis=new FileInputStream(sFileName);
            fos=new FileOutputStream(dFileName);
            int len=-1;
            while((len=fis.read(buffer, 0, buffer.length))!=-1){
                //fis.read(byte[] b,int off,int len)的返回值是int类型
                //此处len即为每次读取到的字符个数,当读取为空是是-1
                //三个参数分别是:缓存数组   开始位置   每次存到缓存数组的长度
                //**************************************************
                //参数列表(char数组,开始的下标,写入的长度)
                fos.write(buffer, 0, len);
                fos.flush();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                if(fis!=null){
                    fis.close();                    
                }
                if(fos!=null){
                    fos.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }






}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值