«

java4Android(15)处理流的使用示例、装饰者设计模式、节点流和处理流的关系

时间:2024-3-2 19:45     作者:韩俊     分类: Android


BufferedReader使用方法

import java.io.*;
class test
{
    public static void  main(String args[])
    {
        FileReader fileReader = null;
        BufferedReader br = null;
        try
        {
            fileReader = new FileReader("D:/BaiduYunDownload/Java4Android/lx_1/from.txt");
            br = new BufferedReader(fileReader);
            String line = null;
            while(true)
            {
                line = br.readLine();
                if(br.readLine() == null)
                {
                    break;
                }
                System.out.println(line);
            }

        }
        catch(Exception e)
        {
            System.out.println(e);
        }
        finally
        {
            //fileReader.close();
            //br.close();
        }
    }
}

装饰者模式实例:

class test
{
    public static void main(String[] args)
    {
        //目标 要一个A公司的水管工
        //水管工
        Plumber p = new Plumber();
        //A公司
        AWorker aworker = new AWorker(p);
        aworker.doSomeThing();
        //木工
        //Carpenter c = new Carpenter();
        //AWorker a = new AWorker(c);
        //a.doSomeThing();
    }
}
interface Worker
{
    public void doSomeThing();
}
class Plumber implements Worker
{
    public void doSomeThing()
    {
        System.out.println("水管工修水管");
    }
}
class Carpenter implements Worker
{
    public void doSomeThing()
    {
        System.out.println("木匠做门窗");
    }
}
class AWorker implements Worker
{
    //重点
    private Worker mWorker;
    AWorker(Worker worker)
    {
        this.mWorker = worker;
    }
    public void doSomeThing()
    {
        System.out.println("a company say ni hao");
        mWorker.doSomeThing();
    }
}

结果:

a company say ni hao
水管工修水管
        <p>版权声明:本文为博主原创文章,未经博主允许不得转载。</p>

标签: android

热门推荐