




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验四 Java 输入输出流1实验目的(1) 掌握输入输出流的总体结构;(2) 掌握流的概念;(3) 了解各种流(包括文件流、过滤流、对象的序列化、随机访问)的使用。2实验内容实验题1 编写一个Java Application程序,打印命令行输入的所有参数。基本要求 编写完整程序。运行结果:代码如下:import java.util.Scanner;public class CommandOutPut /* * param args */public static void main(String args) / TODO Auto-generated method stubSystem.out.println(Please input :);Scanner in = new Scanner(System.in);String str = in.nextLine();System.out.println(The output results :);System.out.println(str);in.close();实验题2 通过键盘输入路径,搜索指定路径下的全部内容。运行结果:代码如下:package .output;import java.io.File;import java.io.FileInputStream;import java.io.IOException;public class Output /* * param args * throws IOException */public static void main(String args) throws IOException / TODO Auto-generated method stubString fileName = d:xxx.txt;File file = new File(fileName);byte b=new byte(int)file.length();FileInputStream out=new FileInputStream(file);out.read(b);out.close();String s=new String(b);/将字节流转换为字符串System.out.println(s); 实验题3设计一个类FileRWTest,实现从input.txt文件中读入数据到字符数组cBuffer中,然后再写入到文件“output.txt”中。运行结果:代码:package .fileRWTester;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class FileRWTest /* * param args * throws IOException */public static void main(String args) throws IOException / TODO Auto-generated method stubBufferedReader source = new BufferedReader(new FileReader(d:input.txt);BufferedWriter target = new BufferedWriter(new FileWriter(d:output.txt);/byte cBuffer = new byte(int)(CharSequence) source).length();String temp = null;/将字节流转换为字符串while (temp = source.readLine() != null) target.write(temp);target.newLine();target.flush();source.close();target.close();实验题4 建立一个书籍信息的文本文件,其中包括编号、书籍名称、版本、价格、销售额字段及5本书籍的记录。编写程序读入书籍信息文件并将第3本、第4本书籍价格分别增加20和30,再将修改后的书籍信息文件输出到另一个文本文件中(文件名称为book.txt)。文本文件book.txt内容如下:编号 名称 版本 价格 销售额1001Java程序设计第2版56.95601002 Java开发实战第1版98.98201003C+程序设计指南第3版62.53621004EJB3.0入门经典第1版59.812801005 Spring3.0 in Action 第3版 95.8 1189设计思路:首先建立一个Book类,定义属性private String num,private String name,private String edition,private Float price,private Float slaes,在主函数中创建5个实例,并把值赋给String text,然后调用target.write()函数写入文件book里。运行结果:检测是否写入,用read()函数读出:代码:package .Input.tester;import java.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;import .Input.clas.Book;public class InputTester /* * param args * throws IOException */public static void main(String args) throws IOException / TODO Auto-generated method stubBook b1 = new Book(1001, Java程序设计, 第2版, 56.9F, 560f);Book b2 = new Book(1002, Java开发实战, 第1版, 98.9f, 820f);Book b3 = new Book(1003, C+程序设计指南, 第3版, 62.5f, 362f);Book b4 = new Book(1004, EJB3.0入门经典, 第1版, 59.8f, 1280f);Book b5 = new Book(1005, Spring3.0 in Action, 第3版 , 95.8f, 1189f);Book books = b1, b2, b3, b4, b5 ;String text = null;for (int index = 0; index books.length; +index) text += booksindex;text += n;BufferedWriter target = new BufferedWriter(new FileWriter(d:book.txt);target.write(text);target.flush();问题:忘记在Book类中些toString函数,故导致每次写入都不是想要写入的东西。实验题5 有四个类,主类Store在包.nwsuaf.jp.p4中,Mobile、Mp3Player、Product在包.nwsuaf.jp.p4.data中,Mobile、Mp3Player是Product类的子类, Product类实现Seralizable接口。基本要求:(1)在Store类中用ObjectOutputStream类的对象把Mobile、Mp3Player类对象输出到文件“product.txt”中。(2)在Store类中用ObjectInputStream类的对象从文件“product.txt”输入数据并将其输出。实验设计:在product中重写writeObject和raedObject函数,并在主函数中调用writeObject和raedObject函数对文件读写。实验结果:写入文件的 从文件中读出的:代码:Product中的writeObject和raedObject函数:private void writeObject(ObjectOutputStream oos) throws IOException oos.defaultWriteObject();oos.writeBytes(getName();oos.writeFloat(getPrice();private void readObject(ObjectInputStream ois) throws IOException,ClassNotFoundException ois.defaultReadObject();主函数:package .nwsuaf.jp.p4;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import .nwsuaf.jp.p4.data.Mobile;import .nwsuaf.jp.p4.data.Mp3Player;public class Store /* * param args */public static void main(String args) throws IOException,ClassNotFoundException try Mp3Player p1 = new Mp3Player(Meizo X3 (256MB), 399.0f);Mp3Player p2 = new Mp3Player(Meizo E5 (512MB), 580.0f);Mp3Player p3 = new Mp3Player(Xlive XM Mp3Play(256MB), 930.0f);Mobile m1 = new Mobile(E365 on China Mobile, 1780.0f);Mobile m2 = new Mobile(E3330 on China Mobile, 1450.0f);ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(d:product.txt);oos.writeObject(p1);oos.writeObject(p2);oos.writeObject(p3);oos.writeObject(m1);oos.writeObject(m2);oos.close();ObjectInputStream ois = new ObjectInputStream(new FileInputStream(d:product.txt);while (ois.readObject() != null) System.o
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年护士中级面试技巧及护理实操模拟题答案全攻略
- 2025年化工工艺专业基础与炼油装置操作实践模拟题集
- 2025年仓库安全员招聘面试题库从基础到进阶
- 2025年炼油装置中级操作工面试题集与答案解析
- 2025年水资源保护与生态流量管理实战手册与考试题库及答案
- 2025年销售代表初级面试模拟题及答案全收录
- 电剪安全知识培训课件
- 2025年财务管理主管竞聘面试题集与答案
- 2025年仓库设备维护与操作笔试模拟题及答案解析
- 2025年烹饪技艺初级考核试题集
- 美宜佳转让协议合同
- 混改公司合同协议模板
- 儿童多种维生素课件
- GA/T 2159-2024法庭科学资金数据清洗规程
- 江苏常州2025年公开招聘农村(村务)工作者笔试题带答案分析
- 2025年职工职业技能竞赛(物业管理师)参考试题(附答案)
- 维修框架协议书范本
- 成人肠造口护理要点与实践课件
- 行李员行李员试卷(练习题库)
- 会务服务面试题及答案
- 电力安全监护培训课件
评论
0/150
提交评论