黑马程序员 java学习笔记3-IO流之字节流与装饰设计模式

---------------------- android培训java培训、期待与您交流! ----------------------

装饰设计模式:

当想要对已有的对象功能进行增强时,可以定义一个类,将已有对象传入,基于已有的功能,并提供加强功能,那么该自定义的类就称为装饰类。

装饰类通常会以构造方法的方式接收需要被装饰的类。

class Person{
    public void chifan(){
        System.out.println("吃饭");
    }
}
class SuperPerson{
    private Person p;
    SuperPerson(Person p){
        this.p = p;
    }
    public void superchifan(){
        System.out.println("喝酒");
        p.chifan();
        System.out.println("吃水果");
    }
}
public class Test1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Person p = new Person();
        SuperPerson sp = new SuperPerson(p);
        sp.superchifan();
    }

}

--------------------------------------------------------------------------------------------------------

(2)、字节流

InputStream(读),OutputStream(写)

public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
    //    writerStream3();        // 一般情况下 不建议使用这种定义一个大小刚好的数组方式。
        readerStream2();
    }
    
    public static void readerStream3() throws Exception
    {
        FileInputStream fin = new FileInputStream("E:\\aa.txt");
        byte [] by = new byte[fin.available()];   //定义一个大小刚好的缓冲区
        int num = fin.read(by);
        System.out.println(new String(by,0,num));
    }

    public static void readerStream2() throws Exception
    {
        FileInputStream fin = new FileInputStream("E:\\aa.txt");
        byte [] buf = new byte[1024];
        int num = 0;
        while((num=fin.read(buf))!=-1)
        {
            System.out.println(new String(buf,0,num));
        }
        fin.close();
    }
    public static void writerStream() throws Exception
    {
        FileOutputStream fout = new FileOutputStream("E:\\aa.txt",true);
        fout.write("我爱你".getBytes());
        fout.close();

    }

2。

        FileOutputStream fw = null;
        FileInputStream fi = null;    
        try {
            fi = new FileInputStream("E:\\Project1.exe");
            fw = new FileOutputStream("E:\\copy_Project1.exe");
            
            byte [] buf = new byte[1024];
            int num = 0;
            while((num=fi.read(buf))!=-1)
            {
                fw.write(buf);
            }
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println(e.toString());
        }
        finally
        {
            if(fw!=null)
            {
                try {
                    fw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    throw new RuntimeException("复制文件失败");
                }
            }
            if(fi!=null)
            {
                try {
                    fi.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    System.out.println(e.toString());
                }
            }
        }

(3)、读取键盘录入

         InputStream in = System.in;
        StringBuilder sb = new StringBuilder();
        while(true)
        {
            int iread = in.read();
            if(iread=='\r')
            {
                continue;
            }
            if(iread=='\n')
            {
                String s = sb.toString();
                
                if(s.equals("over"))           //当输入“over”时结束
                    return;
                System.out.println(s);
                sb.delete(0, sb.length());     //清空缓冲区
            }else{
                sb.append((char)iread);
            }
        }

-----------------例子二---------------------------------

       //获取键盘录入对象
        InputStream in = System.in;
        //将字节流对象转换成字符流对象,使用转换流
        InputStreamReader isr = new InputStreamReader(in);
        //为了提高效率,将字符串进行缓冲区技术高校操作,使用BufferedReader
        BufferedReader bufr = new BufferedReader(isr);
        String line = null;
        while((line=bufr.readLine())!=null)
        {
            System.out.println(line);
        }
        bufr.close();

//键盘的最常用写法

        BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));
        String line = null;
        while((line=bufr.readLine())!=null)
        {
            if(line.equals("over"))
                break;
            bufw.write(line);
            bufw.newLine();
            bufw.flush();
        }
        bufw.flush();
        bufr.close();


---------------------- android培训java培训、期待与您交流! ----------------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值