• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
暮霭晨星
博客园    首页    新随笔    联系   管理    订阅  订阅

Java第九次作业

(一)学习总结

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

(二)实验总结
实验内容:
1.宠物商店:在实验八的基础上,增加一个功能,用文件保存每日的交易信息记录。
2.完成文件复制操作,在程序运行后,提示输入源文件路径和目标文件路径。
完成实验内容,代码上传到码云,注意,宠物商店要求务必将创建数据库的脚本文件随项目文件一起上传,在随笔中分析程序设计思路,用PowerDesigner画出类图结构,并对完成实验内容过程中遇到的问题、解决方案和思考等进行归纳总结,注意代码中必须有必要的注释。

格式如下:

程序设计思路:首先,创建进入界面,选择注册或登录;点击注册,进入注册界面,进行注册;进行新用户注册,调转到登录界面,或者点击登录直接进入登录界面,显示宠物管理页面,显示宠物清单
创建一个主方法类,建vo包宠物类,用户类,图片,工具类,view包用户界面,管理员界面,注册界面,登录界面链接数据库对上次的程序作修改。增加购买功能用文件保存每日的交易信息记录
类图结构:

问题1:实现Java和文件的链接

public static void savePets(SellPet 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 fruit 水果信息
*/
public static void createFile(String name,boolean label,SellPet 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.getNo()).append(SEPARATE_FIELD);
sbf.append(pet.getVariety()).append(SEPARATE_FIELD);
sbf.append(pet.getAge()).append(SEPARATE_FIELD);
sbf.append(pet.getAmount()).append(SEPARATE_FIELD);
sbf.append(pet.getMoney()).append(SEPARATE_FIELD);
String str = sbf.toString();
byte[] b = str.getBytes();
for(int i = 0; i < b.length;i++){
out.write(b[i]);
}
}catch (Exception e) {
e.printStackTrace();
}finally{
if(out != null)
{
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
问题2:销售记录的复制,配置完整文件路径。

File fSource=new File("d:/javacode/Pet_JDBC_IO"+File.separator+"销售记录20170524.csv");
File dSource =new File("d:/javacode/Pet_JDBC_IO"+File.separator+"2017销售记录.csv");
if (!fSource.exists()) {
System.out.println("源文件不存在");
System.exit(1);
}

(三)代码托管(务必链接到你的项目)

https://git.oschina.net/hebau_cs15/FMM.git

码云commit历史截图
上传实验项目代码到码云,在码云项目中选择“统计-commits”,设置搜索时间段,搜索本周提交历史,并截图。

posted on 2017-05-25 18:27  暮霭晨星  阅读(284)  评论(3)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3