Java IO流 之 BufferedReader BufferedWriter 实例

Posted verejava

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java IO流 之 BufferedReader BufferedWriter 实例相关的知识,希望对你有一定的参考价值。

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 实例的主要内容,如果未能解决你的问题,请参考以下文章

Java IO流之字符缓冲流

J06-Java IO流总结六 《 BufferedReader和BufferedWriter 》

IO流操作之字符输入输出流简单操作

Java 之 FileReader FileInputStream InputStreamReader BufferedReader 作用与区

JAVA IO流相关代码(字符流:FileWriter类,FileReader类,BufferedReader类,BufferedWriter类)

Java之IO操作总结