模拟QQ相册上传图片(上传指定文件夹下所有的图片)

package cnm.hp.zy;

import java.io.*;

public class Copy {
        public static void main(String[] args) throws IOException {
            // 封装目录
            File srcFolder = new File("D:\\test");
            //封装目的地
            File desFolder = new File("D:\\test2");
            //目的地文件夹不存在,电脑不会自动创建
            //所以当文件夹不存在,就创建
            if(!desFolder.exists()) {//检测是否有文件夹
                desFolder.mkdirs();//创建文件夹
            }

            //获取该文件夹下的所有文本的File数组
            File[] fileArray = srcFolder.listFiles();

            //遍历该File数值,得到每一个File对象
            for(File file : fileArray) {
                //System.out.println(file);

                String name = file.getName();//1.jpg
                File newFile = new File(desFolder,name);

                copy(file,newFile);

            }
        }

        private static void copy(File file, File newFile) throws IOException {
            BufferedInputStream bi = new BufferedInputStream(
                    new FileInputStream(file));
            BufferedOutputStream bo = new BufferedOutputStream(
                    new FileOutputStream(newFile));

            byte[] by = new byte[1024];
            int len = 0;
            while((len = bi.read(by)) != -1) {
                bo.write(by,0,len);
            }
            bo.close();
        }
    }


将“柜台商品管理”数据保存到本地文件中,原本的Goods[]数组作为柜台删除此行,现改为本地文件保存。

package cnm.hp.zy;

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

public class Couste {
    private Goods[] goodses = new Goods[10];
    int num;
    /*public static void main(String[] args) throws IOException{
                //确认源文件
                File file=new File("../untitled/goods.txt");
                //确认输入流
                FileReader fr=new FileReader(file);
                //读数据
                int n;
                StringBuilder sb=new StringBuilder();
                while ((n=fr.read())!=-1){
                    sb.append((char)n);
                }
                //及时关闭输入
                fr.close();

                //确定输出目标
                File file2=new File("D:/test/good.txt");
                //确认输出流
                FileWriter fw=new FileWriter(file2);
                //写数据
                for (int i = 0; i <sb.length() ; i++) {
                    char a=sb.charAt(i);
                    fw.write(a);
                }
                //及时关闭输出
                fw.close();
            }*/
        private void show() throws IOException {
            //创建字符流过滤流输入流读取(读)
            FileReader in = new FileReader("../untitled/goods.txt");//字符流
            BufferedReader br = new BufferedReader(in);//封装为过滤流
        //开始读取
            int i = 0;
            String line = null;
            while ((line= br.readLine())!=null){
                String data[] =line.split(",");
                this.goodses[i] = new Goods(Integer.parseInt(data[0]),data[1],Double.parseDouble(data[2]),data[3],data[4]);
                i++;
                this.num++;
            }


            FileWriter out = new FileWriter("D:/test/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].getNum()+","+this.goodses[j].getName()+","+this.goodses[j].getPrice()+","+this.goodses[j].getDesc()+","+this.goodses[j].getDesc2());//格式:1001,肠粉,6,真好吃啊,那些吃不到的人真可怜..
                    bw.newLine();
                    System.out.println(goodses[j]);
                }
            }
            //关闭流,释放资源
            br.close();
            in.close();
            bw.close();
            out.close();
            return;
        }
   
    public static void main(String[] args) throws IOException {
        Couste cs =  new Couste();
        cs.show();
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值