package Demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Tu {
public static void main(String[] args) throws IOException {
File f = new File("E:\\图片\\");
String[] f1 = f.list();
for (int i = 0; i < f1.length; i++) {
FileInputStream f2= new FileInputStream("E:\\图片\\"+f1[i]);
FileOutputStream f3 = new FileOutputStream("D:\\休闲娱乐\\壁纸\\"+f1[i]);
int date = 0;
if ((date=f2.read()) != -1){
f3.write(date);
}
f3.close();
f2.close();
}
}
}
将“柜台商品管理”数据保存到本地文件中,原本的Goods[]数组作为柜台删除此行,现改为本地文件保存。
package Demo;
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();
}
}
}