Java IO流 之 BufferedReader BufferedWriter 執行個體

來源:互聯網
上載者:User

標籤:股份   anti   tco   etc   buffer   out   i++   date   ali   

http://www.verejava.com/?id=16994737475720

package com.bufferedwriter;import java.io.BufferedWriter;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.util.ArrayList;import java.util.List;/* * 藥品名稱 規格      生產廠家                有效日期        數量葡萄糖注射液  50ml:5g 浙江都邦藥業股份有限公司    2012-12-14  1000熊去氧膽酸       40ml:5g 四川協力製藥有限公司      2012-12-11  450 */public class TestBufferedWriter{    public static void main(String[] args)    {        List<Medicine> list=new ArrayList<Medicine>();        list.add(new Medicine("葡萄糖注射液","50ml:5g","浙江都邦藥業股份有限公司","2012-12-14",1000));        list.add(new Medicine("熊去氧膽酸","40ml:5g","四川協力製藥有限公司","2012-12-11",450));                BufferedWriter bw=null;        try        {            bw=new BufferedWriter(new FileWriter(new File("res/bufferedwriter/medicine.txt")));            for(int i=0;i<list.size();i++)            {                Medicine item=list.get(i);                                bw.write(item.getName()+","+item.getFormat()+","+item.getCompany()+","+item.getValidDate()+","+item.getQuantity());                bw.newLine();            }            bw.flush();                    } catch (IOException e)        {            e.printStackTrace();        }        finally        {            try            {                bw.close();            } catch (IOException e)            {                e.printStackTrace();            }        }            }}package com.bufferedwriter;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.List;/* * 藥品名稱 規格      生產廠家                有效日期        數量葡萄糖注射液  50ml:5g 浙江都邦藥業股份有限公司    2012-12-14  1000熊去氧膽酸       40ml:5g 四川協力製藥有限公司      2012-12-11  450 */public class TestBufferedReader{    public static void main(String[] args)    {        List<Medicine> list=new ArrayList<Medicine>();                BufferedReader br=null;        try        {            br=new BufferedReader(new FileReader(new File("res/bufferedwriter/medicine.txt")));            String str=null;            while((str=br.readLine())!=null)            {                String[] strArray=str.split(",");                String name=strArray[0];                String format=strArray[1];                String company=strArray[2];                String validDate=strArray[3];                int quantity=Integer.parseInt(strArray[4]);                list.add(new Medicine(name,format,company,validDate,quantity));            }                    } catch (IOException e)        {            e.printStackTrace();        }        finally        {            try            {                br.close();            } catch (IOException e)            {                e.printStackTrace();            }        }                for(int i=0;i<list.size();i++)        {            Medicine item=list.get(i);            System.out.println(item.getName()+","+item.getFormat()+","+item.getCompany()+","+item.getValidDate()+","+item.getQuantity());        }            }}package com.bufferedwriter;public class Medicine{    private String name;    private String format;    private String company;    private String validDate;    private int quantity;    public Medicine(String name, String format, String company,            String validDate, int quantity)    {        super();        this.name = name;        this.format = format;        this.company = company;        this.validDate = validDate;        this.quantity = quantity;    }    public String getName()    {        return name;    }    public void setName(String name)    {        this.name = name;    }    public String getFormat()    {        return format;    }    public void setFormat(String format)    {        this.format = format;    }    public String getCompany()    {        return company;    }    public void setCompany(String company)    {        this.company = company;    }    public String getValidDate()    {        return validDate;    }    public void setValidDate(String validDate)    {        this.validDate = validDate;    }    public int getQuantity()    {        return quantity;    }    public void setQuantity(int quantity)    {        this.quantity = quantity;    }            }

http://www.verejava.com/?id=16994737475720

Java IO流 之 BufferedReader BufferedWriter 執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.