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
原文地址:https://www.cnblogs.com/verejava/p/9227227.html
时间: 2024-11-09 00:07:53