java 作业9
java作业9
<一>学习总结
1.用思维导图对javaIO操作的学习内容进行总结。

2.下面的程序实现了文件的拷贝,但采用的是一个字节一个字节的读写方式,效率很低。使用缓冲区可以减少对文件的操作次数,从而提高读写数据的效率。IO包中提供了两个带缓冲的字节流BufferedInputStream和BufferedOutputStream,查阅JDK帮助文档,修改程序,利用这两个类完成文件拷贝,对比执行效率
import java.io.*;
public class Test{
public static void main(String args[]) {
FileInputStream in=null;
FileOutputStream out=null;
File fSource=new File("d:"+File.separator+"my.jpg");
File fDest=new File("d:"+File.separator+"java"+File.separator+"my.jpg");
if(!fSource.exists()){
System.out.println("源文件不存在");
System.exit(1);
}
if(!fDest.getParentFile().exists()){
fDest.getParentFile().mkdirs();
}
try {
in=new FileInputStream(fSource);
out=new FileOutputStream(fDest);
int len=0;
long begintime = System.currentTimeMillis();
while((len=in.read())!=-1){
out.write(len);
}
long endtime = System.currentTimeMillis();
System.out.println("文件拷贝完成,耗时"
+(endtime-begintime)+"毫秒");
}catch(Exception e){
System.out.println("文件操作失败");
}finally{
try {
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

改正后的的代码
import java.io.*;
public class Test {
public static void main(String args[]) {
FileInputStream in = null;
FileOutputStream out = null;
File fSource = new File("d:" + File.separator + "my.jpg");
File fDest = new File("d:" + File.separator + "新建文件夹" + File.separator + "my.jpg");
if (!fSource.exists()) {
System.out.println("源文件不存在");
System.exit(1);
}
if (!fDest.getParentFile().exists()) {
fDest.getParentFile().mkdirs();
}
try {
in = new FileInputStream(fSource);
out = new FileOutputStream(fDest);
byte[] buff = new byte[1024];
int len = 0;
long begintime = System.currentTimeMillis();
while ((len = in.read(buff)) != -1) {
out.write(buff, 0, len);
}
long endtime = System.currentTimeMillis();
System.out.println("文件拷贝完成,耗时" + (endtime - begintime) + "毫秒");
} catch (Exception e) {
System.out.println("文件操作失败");
} finally {
try {
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

通过对比,用BufferedInputStream和BufferedOutputStream方法复制文件效率快。
<二>实验总结
实验内容:
1.宠物商店:在实验八的基础上,增加一个功能,用文件保存每日的交易信息记录。
2.完成文件复制操作,在程序运行后,提示输入源文件路径和目标文件路径。
1.对excel表的命名
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
String name = "销售记录" + format.format(date)+ ".csv";
2.将数据保存在表中
public void buypetItem(String cno, int number) {
Petshop pet = adminService.buy(cno);
if (pet != null) {
if (pet.getNumber() < number) {
JOptionPane.showMessageDialog(this, "现货没有这么多了。请重新输入");
//addCnoText.setText("");
addNumberText.setText("");
addNumberText.requestFocus();
} else {
pet.setNumber(pet.getNumber() - number);
boolean updateSuccess = admin.updatePetNumber(cno, pet.getNumber());
if (updateSuccess) {
queryPetItem();
JOptionPane.showMessageDialog(this, "购买成功");
// buyNumberText.setText("");
addCnoText.setText("");
addNumberText.setText("");
// Double money = pet.getPrice()*amount;
Buy spet = new Buy(pet.getCno(), pet.getKind(),
pet.getPrice(),number,pet.getPrice()*number);
pettool.savepets(spet); // 将本次数据保存到本地文件
} else {
JOptionPane.showMessageDialog(this, "购买失败");
}
}
} else {
JOptionPane.showMessageDialog(this, "购买失败");
}
}
3.如何对文件实例化
File fSource = new File(input);
以上代码的input代表着文件的路径。

浙公网安备 33010602011771号