java第九次作业

时间:2022-03-17 22:00:02

(一)学习总结

1.用思维导图对javaIO操作的学习内容进行总结。
java第九次作业

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

BufferedInputStream(InputStream in)
创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。

BufferedOutputStream(OutputStream out)
创建一个新的缓冲输出流,以将数据写入指定的底层输出流。

            int len=0;
long begintime = System.currentTimeMillis();
while((len=in.read())!=-1){
out.write(len);
}
            byte[] buff = new byte[1024];
int len = 0;
long begintime = System.currentTimeMillis();
while ((len = in.read(buff)) != -1) {
out.write(buff, 0, len);
}

java第九次作业

java第九次作业

使用 BufferedInputStream 运行效率
3.其他需要总结的内容。
(二)实验总结

实验内容:
1.宠物商店:在实验八的基础上,增加一个功能,用文件保存每日的交易信息记录。
2.完成文件复制操作,在程序运行后,提示输入源文件路径和目标文件路径。

完成实验内容,代码上传到码云,注意,宠物商店要求务必将创建数据库的脚本文件随项目文件一起上传,在随笔中分析程序设计思路,用PowerDesigner画出类图结构,并对完成实验内容过程中遇到的问题、解决方案和思考等进行归纳总结,注意代码中必须有必要的注释。

宠物数据库:
java第九次作业

登陆界面:
java第九次作业
添加一个宠物信息:
java第九次作业
可以看到在表格文件中有宠物添加记录:
java第九次作业
出现问题:怎样实现java与文件链接?

public static void savepets(PetItem pet) {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
String name = "销售记录" + format.format(date)+ ".csv";
InputStream in = null;
try {
in = new FileInputStream(name); //判断本地是否存在文件
if(in != null){
in.close();
createFile(name,true,pet); // 存在文件,采用修改文件方式
}
} catch (FileNotFoundException e) {
createFile(name,false,pet); // 不存在文件,采用新建文件方式
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 将售出信息保存到本地
* @param name 文件名
* @param label 文件已存在标识 true:存在则修改 false:不存在则新建
* @param pet 水果信息
*/
public static void createFile(String name,boolean label,PetItem pet){
BufferedOutputStream out = null; //字节缓冲流
StringBuffer sbf = new StringBuffer();
try {
if(label){
out = new BufferedOutputStream(new FileOutputStream(name,true));
}else{
out = new BufferedOutputStream(new FileOutputStream(name));
String[] fieldSort = {"宠物编号","宠物种类","宠物年龄","购买数量","购买单价"};
//创建表头
for (String fieldKey : fieldSort){
sbf.append(fieldKey).append(SEPARATE_FIELD);
}
}
sbf.append(SEPARATE_LINE);
sbf.append(pet.getNumber()).append(SEPARATE_FIELD);
sbf.append(pet.getName()).append(SEPARATE_FIELD);
sbf.append(pet.getAge()).append(SEPARATE_FIELD);
sbf.append(pet.getunit()).append(SEPARATE_FIELD);
sbf.append(pet.getPrice()).append(SEPARATE_FIELD);
String str = sbf.toString();
byte[] b = str.getBytes();
for(int i = 0; i < b.length;i++){
out.write(b[i]);
}

出现问题2:怎么样实现复制?

public static void main(String args[]) {
FileInputStream in = null;
FileOutputStream out = null;
System.out.println("请输入要复制的文件路径");//根据用户输入的路径,判断源文件是否存在
Scanner in1 = new Scanner(System.in);
String input=in1.next();
File fSource = new File(input);
if (!fSource.exists()) {
System.out.println("源文件不存在");
System.exit(1);
}
System.out.println("请输入储存路径");
String input2=in1.next();

File fDest = new File(input2);
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); //运用FileIputStream()和FileOutputStream()方法,使用缓冲区对文件进行读写
}
long endtime = System.currentTimeMillis();
System.out.println("文件拷贝完成, 耗时" + (endtime - begintime) + "毫秒");

代码托管:
链接:https://git.oschina.net/hebau_cs15/java-cs02mhj.git
java第九次作业