2022/07/15作业2

2、将“柜台商品管理”数据保存到本地文件中,原本的Goods[]数组作为柜台删除此行,现改为本地文件保存。
    如:goods.txt,参考数据
        1001,肠粉,6,真好吃啊,那些吃不到的人真可怜..
        1002,辣条,2,真好吃啊,那些吃不到的人真可怜..
        1003,巧克力,36,真好吃啊,那些吃不到的人真可怜..

/**
 * 展示柜台所有的商品(不能输出null)
 */
private void show(){
    System.out.println("-------------  柜台商品信息  ---------------");
    //创建字符流过滤流输入流读取
    FileReader in = new FileReader("goods.txt");//字符流(提前创建goods.txt)
    BufferedReader br = new BufferedReader(in);//封装为过滤流
    
    //开始读取
    String line = null;//readLine()一次读取一行的字符粗
    while((line = br.readLine())!=null){
        System.out.println("line = " + line);
    }
    //关闭流,释放资源
    br.close();
    in.close();
    System.out.println("------------------------------------------");
}


【参考代码-读取到数组中】
//创建字符流过滤流输入流读取(读)
FileReader in = new FileReader("goods.txt");//字符流
BufferedReader br = new BufferedReader(in);//封装为过滤流
//创建字符流过滤流输出流读取(写)
FileWriter out = new FileWriter("goods.txt");//字符流
BufferedWriter bw = new BufferedWriter(out);//封装为过滤流

//开始读取
int i = 0;
String line = null;//readLine()一次读取一行的字符粗
while((line = br.readLine())!=null){
    //把读取的数据放到数组中
    String[] data = line.split(",");//根据,拆分数据 1001,肠粉,6,真好吃啊,那些吃不到的人真可怜..
    //特别注意:数据都存到数组中了
    this.goodses[i] = new Goods(Integer.parseInt(data[0]),data[1],Double.parseDouble(data[2]),data[3]);
    i++;
}
//关闭流,释放资源
br.close();
in.close();

【参考代码-把数组的数据写入到文件】
//创建字符流过滤流输出流读取(写)
FileWriter out = new FileWriter("goods.txt");//字符流
BufferedWriter bw = new BufferedWriter(out);//封装为过滤流

//开始写入
for (int i = 0; i < this.goodses.length; i++) {
    if(this.goodses[i]!=null){
        bw.write(this.goodses[i].getId()+","+this.goodses[i].getGoodsName()+","+this.goodses[i].getPrice()+","+this.goodses[i].getDesc());//格式:1001,肠粉,6,真好吃啊,那些吃不到的人真可怜..
        bw.newLine();
    }
}
//关闭流,释放资源
bw.close();
out.close();

package zy;
package com.hp.demo2;

import java.io.*;
import java.util.Scanner;

public class demo2 {
        int num;
        private Goods[] goodses = new Goods[10];//
        //展示柜台所有的商品(不能输出null)
        private void show() throws IOException {
            System.out.println("====");
            //创建字符流过滤输入流读入
            FileReader in = new FileReader("D:\\goods.txt");
            BufferedReader br = new BufferedReader(in);//封装为过滤流
            //开始读取
            int i = 0;
            String line = null;
            //readLine()一次读取一行的字符粗
            while ((line = br.readLine())!=null) {
                String data[]=line.split(",");
                //注意类型注意类型不然运行都是null和0
                this.goodses[i] = new Goods(Integer.parseInt(data[0]),data[1], (int) Double.parseDouble(data[2]),data[3]);
                i++;
                this.num++;
            }
            //创建字符流过滤输出流读取
            FileWriter out = new FileWriter("D:\\goods.txt");
            BufferedWriter bw = new BufferedWriter(out);
            for (int j = 0; j < this.goodses.length; j++) {
                if (this.goodses[j]!=null){
                    bw.write(this.goodses[j].getId()+","+this.goodses[j].getGoodsName()+","+this.goodses[j].getPrice()+","+this.goodses[j].getDesc());
                    bw.newLine();//newLine() 写行分隔符。
                    System.out.println(goodses[j]);
                }
            }
            br.close();
            in.close();
            bw.close();
            out.close();
            System.out.println("货架目前商品数量"+num);
            return;
        }
        public void main() throws IOException {
            while(true){
                System.out.println("-------  1.展示商品    2.上架商品    ------");
                System.out.println("-------  3.下架商品    4.调整价格    ------");
                System.out.println("-------          0.退出             ------");
                //选择功能,控制台输入
                System.out.println("-->请输入功能编号:");
                int key = new Scanner(System.in).nextInt();
                //必须是0-4
                if(key<0||key>4){
                    System.out.println("-->警告:输入非法数字!!请重新输入");

                }
//根据需求跳转到相应的方法里面
                switch (key){
                    case 1:show();break;
                    case 0:System.exit(0);
                }
            }
        }
        public static void main(String[] args) throws IOException {
            demo2 d = new demo2();
            d.show();
        }

    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值