java第九次作业

Java第九次作业

(一)学习总结

1.用思维导图对本周的学习内容进行总结。

参考资料: XMind。
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 extends FilterInputStream
BufferedInputStream 为另一个输入流添加一些功能,即缓冲输入以及支持 mark 和 reset 方法的能力。在创建BufferedInputStream时,会创建一个内部缓冲区数组。在读取或跳过流中的字节时,可根据需要从包含的输入流再次填充该内部缓冲区,一次填充多个字节。mark操作记录输入流中的某个点,reset 操作使得在从包含的输入流中获取新字节之前,再次读取自最后一次 mark 操作后读取的所有字节。采用的是一个字节一个字节的读写方式,执行效率低。

BufferedOutputStream extends FilterOutputStream
该类实现缓冲的输出流。通过设置这种输出流,应用程序就可以将各个字节写入底层输出流中,而不必针对每次字节写入调用底层系统,执行效率高。
改进方法:

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;
        byte[] b = new byte[1024];
        long begintime = System.currentTimeMillis();
        while((len=in.read(b))!=-1){
            out.write(b,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();
        }      
    }     
}
}

3.其他需要总结的内容。

  1. 流分为输入流和输出流:(1) 输入流连在某个产生数据的设备上,从输入流中读取数据; (2)输出流连在某个接收数据的设备上,把结果写出到输出流中。
    输入输出流基本的操作原理:
    (1)使用File类找到一个文件
    (2)通过字节流或字符流的子类进行对象的实例化
    (3)进行读或写的操作
    (4)关闭字节或字符流
  2. 打印流: Java的IO包中增加了两种类:PrintStream(字节打印流)、PrintWriter(字符打印流)。 PrintStream类可以使用print/println及write方法。
    因此,在程序输出时一般使用PrintStream。但是,如果输出的数据是通过InputStream对象获得的,则输出用OutputSteam。

(二)实验总结

实验内容:
1.宠物商店
使用JDBC实现宠物商店的基础上,增加一个功能,用文件保存每日的交易信息记录。
利用public boolean createNewFile() throws IOException :创建文件,在文件中建立接口
2.完成文件复制操作,在程序运行后,提示输入源文件路径和目标文件路径。
显示路径的方法

if(file.exists()){
       file.delete();
}else{
try {
file.createNewFile();
System.out.println("文件路径"+file.getAbsolutePath());
System.out.println("文件名:"+file.getName()); System.out.println("文件大小:"
                                                 +file.length()/1024+"KB");
System.out.println("最后一次修改日期:" 
                                             +new Date(file.lastModified()));           
} catch (IOException e) {
	e.printStackTrace();
}

(三)代码托管

  • 码云commit历史截图
原文地址:https://www.cnblogs.com/junjun137/p/6901255.html