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