1、模拟QQ相册上传图片(上传指定文件夹下所有的图片)
把指定文件夹下的所有图片,复制另一个文件夹
目录1:要上传图片的目录
目录2:要接收图片的目录
public class sc {
public static void main(String[] args) throws IOException {
File file = new File("D:\\Test\\");
String s [] = file.list();
for (int i = 0; i < s.length; i++) {
System.out.println(s[i]);
FileInputStream inputStream = new FileInputStream("D:\\Test\\" + s[i]);
FileOutputStream outputStream = new FileOutputStream("D:\\Test\\cs\\" + s[i]);
int a = 0;
while ((a = inputStream.read()) != -1) { //
outputStream.write(a);
}
outputStream.close();
inputStream.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();
public class good {
int num;
private Goods[] goodses = new Goods[10];//
private void show() throws IOException {
System.out.println("====");
FileReader fr = new FileReader("D:\\Test\\goods.txt");
BufferedReader br = new BufferedReader(fr);
//开始读取
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], (int) Double.parseDouble(data[2]),data[3]);
i++;
this.num++;
}
FileWriter fw = new FileWriter("D:\\Test\\cs\\goods.txt");
BufferedWriter bw = new BufferedWriter(fw);
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();
fr.close();
bw.close();
fw.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 {
good c = new good();
c.show();
}
}