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(); } } } }
public BufferedInputStream(InputStream in)建立一个 BufferedInputStream 并保存其参数,即输入流 in,以便未来使用。建立一个内部缓冲区数组并将其存储在 buf 中。java
参数:
in - 底层输入流。
数据库
使用BufferedInputStream和BufferedOutputStream类编写程序:数组
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); 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(); } } } }
建立一个新的缓冲输出流,以将数据写入指定的底层输出流。
参数:
out - 底层输出流。工具
System类对IO的支持:System类是一个强大,特殊的类,它提供了标准输入输出、运行时的系统信息等重要工具。System类中定义了三个常量:public static PrintStream err;public static InputStream in;public static PrintStream out;
思惟导图很好的帮助我整理清楚学习的思路,明确学习的内容。学习
1.宠物商店:在实验八的基础上,增长一个功能,用文件保存每日的交易信息记录。
2.完成文件复制操做,在程序运行后,提示输入源文件路径和目标文件路径。测试
完成实验内容,代码上传到码云,注意,宠物商店要求务必将建立数据库的脚本文件随项目文件一块儿上传,在随笔中分析程序设计思路,用PowerDesigner画出类图结构,并对完成实验内容过程当中遇到的问题、解决方案和思考等进行概括总结,注意代码中必须有必要的注释。
格式以下:
程序设计思路:新建一个SellPetItem类,存储用户购买信息。新建一个FileUtils类将用户购买信息存储到本地文件中,若是存在文件采用修改文件的方式,若是不存在文件采用新建文件的方式。建立项目运行类,定义MainApp方法,进行测试。定义copy类,复制并保存每日的交易信息记录的文件。设计