Ds220702 7.17

 

1、模拟QQ相册上传图片(上传指定文件夹下所有的图片)
    把指定文件夹下的所有图片,复制另一个文件夹
    目录1:要上传图片的目录
    目录2:要接收图片的目录

package com.hp.demo18;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class Demo04 {
    public static void main(String[] args) throws Exception {
        File imgpath = new File("D:\\娱乐\\壁纸");
        File toimgpath = new File("D:\\娱乐\\img");
        File[] imgs = imgpath.listFiles();
        for (int i = 0; i < imgs.length; i++) {
            FileInputStream in = new FileInputStream(imgs[i]);
            File img = new File(toimgpath, imgs[i].getName());
            FileOutputStream out = new FileOutputStream(img);
            int d =0;
            while ((d=in.read())!=-1){
                out.write(d);
            }
            out.close();
            in.close();
        }
    }
}

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();

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值