一、实验目的
1.掌握Java中文件的读写操作。
2.学会使用Java提供的实用类(Vector, ArrayList)来完成特定的功能。
3.掌握字符串类(String, StringBuffer)的使用。
4.掌握用面向对象的方法分析和解决复杂问题。
实验数据:
Inventory.txt
Transactions.txt
三、使用硬件、软件环境
Windows10,内存8g,硬盘1TB,JDK1.8,eclipse
四、实验过程、步骤及原始记录**(算法、原程序、测试结果,分析等)**
源代码:
package inven;
import java.io.*;
import java.lang.*;
import java.util.ArrayList;
import org.omg.CORBA.PUBLIC_MEMBER;
class update{//更新库存信息
void upd() throws IOException{
BufferedReader br = new BufferedReader(
new FileReader(“E:/进货管理/NewInventory.dat”));
BufferedWriter bw = new BufferedWriter(
new FileWriter(“E:/进货管理/Inventory.dat”));
String s = null;
s=br.readLine();
while(s!=null)
{
bw.write(s);
bw.newLine();
s=br.readLine();
}
bw.close(); //用完要关闭
br.close(); //用完要关闭
}
}
class global{
public static int cc=0;
public static int outcount=0;
}
class updateall{//更新库存信息
void updall(String file,String path) throws IOException{
BufferedReader br = new BufferedReader(
new FileReader(file));
BufferedWriter bw = new BufferedWriter(
new FileWriter(path));
String s = null;
s=br.readLine();
while(s!=null)
{
bw.write(s);
bw.newLine();
s=br.readLine();
}
bw.close(); //用完要关闭
br.close(); //用完要关闭
}
}
class updateT{//更新交易记录
void upd() throws IOException{
BufferedReader br = new BufferedReader(
new FileReader(“E:/进货管理/NewTransaction.dat”));
BufferedWriter bw = new BufferedWriter(
new FileWriter(“E:/进货管理/Transaction.dat”));
String s = null;
s=br.readLine();
while(s!=null)
{
bw.write(s);
bw.newLine();
s=br.readLine();
}
bw.close(); //用完要关闭
br.close(); //用完要关闭
}
}
class getshortstring{
String getstring(int i,int j,String s){
int x=0,y=0,z=0,w=0;
String shortstr="";
while(z
}
class Rscan{//交易记录扫描
void updR() throws IOException{
BufferedReader br = new BufferedReader(
new FileReader(“E:/进货管理/Transaction.dat”));
BufferedReader brin = new BufferedReader(
new FileReader(“E:/进货管理/Inventory.dat”));
String s = null;
String invs = null;//对库存行扫描时的变量
String shortstring="";
String shortstringnum="";//记录操作订单的变量
String findinviten="";
String findinvitennum="";
int addnumber=0;
int localnumber=0;
s=br.readLine();
while(s!=null){
if((s.charAt(0)==‘R’)){
getshortstring get=new getshortstring();
shortstring=get.getstring(1,2,s);//获取进出货项目编号
shortstringnum=get.getstring(2,3,s);
addnumber=Integer.parseInt(shortstringnum);
invs=brin.readLine();
while(invs!=null){
findinviten=get.getstring(0,1,invs);
if(shortstring.equals(findinviten)){
findinvitennum=get.getstring(1,2,invs);
localnumber=Integer.parseInt(findinvitennum);
localnumber=localnumber+addnumber;
findinvitennum=localnumber+"";
invs=get.getstring(0,1,invs)+"\t"+findinvitennum+"\t"+get.getstring(2,3,invs)+"\t"+get.getstring(3,4,invs)+"\t";
//按行修改指定出的代码
change ch=new change();
String fileway="E:/进货管理/Inventory.dat";
String destfileway="E:/进货管理/NewInventory.dat";
ch.cha(fileway,destfileway,0,1,shortstring , invs);//处理完到货条款
invs="//"+"\t"+s+"\t";
fileway="E:/进货管理/Transaction.dat";
destfileway="E:/进货管理/NewTransaction.dat";
shortstring="R";
ch.cha(fileway,destfileway,0,1,shortstring , invs);//删除掉已处理的到货条款
///
}
invs=brin.readLine();
}
}
s=br.readLine();
}
br.close(); //用完要关闭
}
}
class change{
void cha(String orfile,String destfile,int x,int y,String find,String exchange) throws IOException{//修改文件地址及特征值
BufferedReader br = new BufferedReader(
new FileReader(orfile));
BufferedWriter bw = new BufferedWriter(
new FileWriter(destfile));
String s = null;
s=br.readLine();
while(s!=null)
{ String item;
getshortstring get=new getshortstring();
item=get.getstring(x,y,s);
if(item.equals(find)){
bw.write(exchange);
bw.newLine();
s=br.readLine();
//
}
else {
bw.write(s);
bw.newLine();
s=br.readLine();
}
}
bw.close(); //用完要关闭
br.close(); //用完要关闭
}
}
class Ascan{//新增货物记录扫描
void upd() throws IOException{
BufferedReader br = new BufferedReader(
new FileReader("E:/进货管理/Transaction.dat"));
BufferedReader brin = new BufferedReader(
new FileReader("E:/进货管理/Inventory.dat"));
BufferedWriter bwin = new BufferedWriter(
new FileWriter("E:/进货管理/NewInventory.dat"));
String s = null;
String invs = null;//对库存行扫描时的变量
String shortstring="";
String shortstringnum="";//记录操作订单的变量
String findinviten="";
String findinvitennum="";
int addnumber=0;
int localnumber=0;
s=br.readLine();
boolean charge=true;
while(s!=null){
if((s.charAt(0)=='A')){
getshortstring get=new getshortstring();
shortstring=get.getstring(1,2,s);//获取新增货项目编号
addnumber=Integer.parseInt(shortstring);//新增货物编号转换为int型
invs=brin.readLine();
charge=true;
while(invs!=null){
findinviten=get.getstring(0,1,invs);
localnumber=Integer.parseInt(findinviten);
if( (addnumber
bwin.write(shortstringnum);
bwin.newLine();
bwin.write(invs);
bwin.newLine();
invs=brin.readLine();
charge=false;
///
String ainvs="//"+"\t"+s+"\t";
String fileway="E:/进货管理/Transaction.dat";
String destfileway="E:/进货管理/NewTransaction.dat";
shortstring="A";
change ch=new change();
ch.cha(fileway,destfileway,0,1,shortstring , ainvs);//删除掉已处理的到货条款
updateT tran=new updateT();
tran.upd();//更新交易记录
}
else{
bwin.write(invs);
bwin.newLine();
invs=brin.readLine();
}
}
}
else{};
s=br.readLine();
}
br.close(); //用完要关闭
brin.close();
bwin.close();
}
}
class oscan{
void updO() throws IOException{
BufferedReader tran = new BufferedReader(
new FileReader(“E:/进货管理/Transaction.dat”));
BufferedReader inve = new BufferedReader(
new FileReader(“E:/进货管理/Inventory.dat”));
//BufferedWriter error = new BufferedWriter(
// new FileWriter(“E:/进货管理/error.dat”));
BufferedWriter ninbw = new BufferedWriter(
new FileWriter(“E:/进货管理/NewInventory.dat”));
//BufferedWriter ship = new BufferedWriter(
// new FileWriter(“E:/进货管理/Ship.dat”));
String trans=null,inves=null,gets=null;
while(global.cc<=global.outcount){
// outtrans=outtran1.readLine();
trans=tran.readLine();
global.cc=global.cc+1;
}
//trans=tran.readLine();
getshortstring get=new getshortstring();
gets=get.getstring(1,2,trans);//获取发出货项目编号
ArrayList itemname=new ArrayList();
ArrayList number=new ArrayList();
ArrayList factory=new ArrayList();
String replace=null;
String oriagn=null;
while(trans!=null&&(trans.charAt(0)=='O')&&(itemname.isEmpty()||itemname.indexOf(gets)!=-1))
{ replace=gets;
oriagn=trans;
itemname.add(get.getstring(1,2,trans));
if(factory.indexOf(get.getstring(3,4,trans))!=-1 ){
int j=0,i=0,k=0;
j=factory.indexOf(get.getstring(3,4,trans));
i=(Integer) number.get(j);
k=Integer.parseInt(get.getstring(2,3,trans));
i=i+k;
number.add(i);
}
else{
number.add(get.getstring(2,3,trans));
factory.add(get.getstring(3,4,trans));
}
trans=tran.readLine();
gets=get.getstring(1,2,trans);
}
///
tran.close();
String ainvs="//"+"\t"+oriagn;
String fileway=“E:/进货管理/Transaction.dat”;
String destfileway=“E:/进货管理/NewTransaction.dat”;
String shortstring=“O”;
ochange cho=new ochange();
cho.ocha(fileway,destfileway,0,1,shortstring , replace,ainvs);//删除掉已处理的出货条款
updateT t=new updateT();
t.upd();//更新交易记录
//
inves=inve.readLine();
int min=0,n=1,k=0;
String chanship=null;
while(inves!=null){
//判断库存对应并进行出货处理,,,记得删除已操作的出货记录
int totalnumber=Integer.parseInt( get.getstring(1,2,inves)) ;
if(itemname.indexOf(get.getstring(0,1,inves))!=-1){
if(totalnumber>=0){
while(number.size()>0){
min=Integer.parseInt (number.get(0).toString());
while(nInteger.parseInt ( number.get(n).toString())){
min=Integer.parseInt ( number.get(n).toString());
n++;
}
else{n++;}
}
k=number.indexOf(Integer.toString(min));//获取要减去的位置
totalnumber=totalnumber-min;
if(totalnumber>0){
inves= get.getstring(0,1,inves)+"\t"+totalnumber+"\t"+ get.getstring(2,3,inves)+"\t"+ get.getstring(3,4,inves)+"\t";
chanship=factory.get(k).toString()+"\t"+itemname.get(0).toString()+"\t"+number.get(k).toString()+"\t";
BufferedReader ship1= new BufferedReader(
new FileReader(“E:/进货管理/ship.dat”));
BufferedWriter newship = new BufferedWriter(
new FileWriter(“E:/进货管理/newship.dat”));
String ships = null;
ships=ship1.readLine();
while(ships!=null)
{
newship.write(ships);
newship.newLine();
ships=ship1.readLine();
}
newship.write(chanship);
newship.newLine();
ship1.close(); //用完要关闭
newship.close(); //用完要关闭
updateall allship=new updateall();
allship.updall(“E:/进货管理/newship.dat”, “E:/进货管理/ship.dat”);
}
else{
totalnumber=totalnumber+min;
chanship=factory.get(k).toString()+"\t"+itemname.get(0).toString()+"\t"+number.get(k).toString()+"\t";
BufferedReader error1= new BufferedReader(
new FileReader(“E:/进货管理/error.dat”));
BufferedWriter newerror = new BufferedWriter(
new FileWriter(“E:/进货管理/newerror.dat”));
String errors = null;
errors=error1.readLine();
while(errors!=null)
{
newerror.write(errors);
newerror.newLine();
errors=error1.readLine();
}
newerror.write(chanship);
newerror.newLine();
error1.close(); //用完要关闭
newerror.close(); //用完要关闭
updateall allerror=new updateall();
allerror.updall(“E:/进货管理/newerror.dat”, “E:/进货管理/error.dat”);
}
number.remove(k);
factory.remove(k);
itemname.remove(k);
}
// ninbw.write(inves);
// ninbw.newLine();
}
}
if(inves!=null){
ninbw.write(inves);
ninbw.newLine();
inves=inve.readLine();
}
}/,,,,
ninbw.close();
//error.close();
//ship.close();
update inv =new update();
inv.upd();//更新库存记录
}
}
class ochange{
void ocha(String orfile,String destfile,int x,int y,String find,String find2,String exchange) throws IOException{//修改文件地址及特征值
BufferedReader br = new BufferedReader(
new FileReader(orfile));
BufferedWriter bw = new BufferedWriter(
new FileWriter(destfile));
String s = null;
s=br.readLine();
while(s!=null)
{ String item;
getshortstring get=new getshortstring();
item=get.getstring(x,y,s);
String item2=get.getstring(1,2,s);
if(item2.equals(find2)){
if(item.equals(find)){
bw.write(exchange);
bw.newLine();
s=br.readLine();
//
}
else {
bw.write(s);
bw.newLine();
s=br.readLine();
}
}
else{ bw.write(s);
bw.newLine();
s=br.readLine();};
}
bw.close(); //用完要关闭
br.close(); //用完要关闭
}
}
class delte{
void del() throws IOException{
BufferedReader delbtr = new BufferedReader(
new FileReader(“E:/进货管理/Transaction.dat”));
String delbtrs=null,dshort=null,invds=null;
delbtrs=delbtr.readLine();
String rep=null;
while(delbtrs!=null)
{
if(delbtrs.charAt(0)=='D'){
rep=delbtrs;
getshortstring getd=new getshortstring();
dshort=getd.getstring(1,2,delbtrs);
BufferedReader invd = new BufferedReader(
new FileReader(“E:/进货管理/Inventory.dat”));
BufferedWriter inb = new BufferedWriter(
new FileWriter(“E:/进货管理/NewInventory.dat”));
invds=invd.readLine();
while(invds!=null){
if(dshort.equals( getd.getstring(0,1,invds))) {
if(getd.getstring(1,2,invds).equals("0")){
invds=invd.readLine();
}
else{
inb.write(invds);
inb.newLine();
///出错处理
BufferedReader error1= new BufferedReader(
new FileReader(“E:/进货管理/error.dat”));
BufferedWriter newerror = new BufferedWriter(
new FileWriter(“E:/进货管理/newerror.dat”));
String errors = null;
errors=error1.readLine();
while(errors!=null)
{
newerror.write(errors);
newerror.newLine();
errors=error1.readLine();
}
newerror.write(“0”+"\t"+getd.getstring(0,1,invds)+"\t"+getd.getstring(1,2,invds)+"\t");
newerror.newLine();
error1.close(); //用完要关闭
newerror.close(); //用完要关闭
updateall allerror=new updateall();
allerror.updall(“E:/进货管理/newerror.dat”, “E:/进货管理/error.dat”);
invds=invd.readLine();
}
}
else{inb.write(invds);
inb.newLine();
invds=invd.readLine();
}
}
invd.close();
inb.close();
}
delbtrs=delbtr.readLine();
}
delbtr.close();
String ainvs="//"+"\t"+rep+"\t";
String fileway=“E:/进货管理/Transaction.dat”;
String destfileway=“E:/进货管理/NewTransaction.dat”;
String shortstring=“D”;
change ch=new change();
ch.cha(fileway,destfileway,0,1,shortstring , ainvs);//删除掉已处理的到货条款
updateT tran=new updateT();
tran.upd();//更新交易记录
}
}
public class Inventory {
public static void main(String[] args)throws Exception{
Rscan find=new Rscan();
find.updR();//完成货物补充
updateT tran=new updateT();
tran.upd();//更新交易记录
update inv =new update();
inv.upd();//更新库存记录
Ascan add=new Ascan();
add.upd();
tran.upd();//更新交易记录
inv.upd();//更新库存记录
// int cc=0;
BufferedReader outtran = new BufferedReader(
new FileReader(“E:/进货管理/Transaction.dat”));
String outtrans=null;
outtrans=outtran.readLine();
// int outcount=0;
outtran.close();
getshortstring outget=new getshortstring();
String outshort=outget.getstring(0,1,outtrans);
while(outtrans!=null){
if(outshort.equals(“O”)){
oscan orpe=new oscan();
orpe.updO();
global.outcount=global.outcount+1;
}
else{global.outcount=global.outcount+1;}
BufferedReader outtran1 = new BufferedReader(
new FileReader(“E:/进货管理/Transaction.dat”));
while(global.cc<=global.outcount+1){
outtrans=outtran1.readLine();
global.cc=global.cc+1;
}
global.cc=0;
outtran1.close();
if(outtrans!=null){
outshort=outget.getstring(0,1,outtrans);
}
}
delte de=new delte();
de.del();
update inve =new update();
inve.upd();//更新库存记录
System.out.println(“处理完毕,请查看结果”);
}
}
运行前:
运行后:
五、实验结论、分析、思考题与心得体会
1.掌握了Java中文件的BufferedReader和BufferedWriter实现输入输出信息的读写操作。
2.学会了使用Java提供的实用类(Vector, ArrayList)来完成特定的功能。
3.掌握了字符串类(String, StringBuffer)的使用。
4.掌握了用面向对象的方法分析和解决复杂问题。