java实验汇编.doc

  1. 1、本文档共10页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
《面向对象程序设计》 实验报告与习题册 2013 / 2014 学年 第1学期 系 别 计算机科学与技术系 专 业 软件工程import java.io.*; public class FileCopy { public static void main(String[]args) { FileInputStream fis = null; FileOutputStream fos = null; BufferedInputStream bis = null; BufferedOutputStream bos = null; int c; try { fis = new FileInputStream(d:/a1.txt); bis = new BufferedInputStream(fis); fos = new FileOutputStream(d:/a2.txt); bos = new BufferedOutputStream(fos); while((c = bis.read())!=-1) bos.write(c); bos.flush(); } catch(FileNotFoundException e1) { System.out.println(e1); } catch(IOException e2) { System.out.println(e2); } finally { try { if(fis !=null) fis.close(); if(fos !=null) fos.close(); if(bis !=null) bis.close(); if(bos !=null) bos.close(); } catch(IOException e3) { System.out.println(e3); } } } } 运行结果 2、定义一个类,把这个类所生成的若干对象写入到文件中,然后从文件中读出对象,测试读出对象的正确性。 import java.io.*; import java.util.*; class Student implements Serializable { String name; char sex; int age; public Student(String name,char sex,int age) { this.name = name; this.sex = sex; this.age = age; } public String getInfo() { return(姓名: + name + \t + 性别: + sex + \t + 年龄: + age); } } public class ObjectFile { public static void main(String[] args) throws Exception { /* Student s1 = new Student(Jack,M,20); Student s2 = new Student(Jom,M,25); FileOutputStream fos = new FileOutputStream(e:\\b.txt); ObjectOutputStream os = new ObjectOutputStream(fos); os.writeObject(s1); os.writeObject(s2); os.close(); */ Student s1; Student s2; FileInputStream fis = new FileInputStream(e:\\b.txt); ObjectInputStream os = new ObjectInputStream(fis); s1 = (Student)os.readObject(); System.out.println(s1.getInfo());

文档评论(0)

希望之星 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档