实验04Java输入输出流报告材料.doc
实验四 Java 输入输出流1实验目的(1) 掌握输入输出流的总体结构;(2) 掌握流的概念;(3) 了解各种流包括文件流、过滤流、对象的序列化、随机访问的使用。2实验内容实验题1 编写一个Java Application程序,打印命令行输入的所有参数。根本要求 编写完整程序。运行结果:代码如下:import java.util.Scanner;publicclass mandOutPut /* * param args */publicstaticvoid 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 .edu.output;import java.io.File;import java.io.FileInputStream;import java.io.IOException;publicclass Output /* * param args * throws IOException */publicstaticvoid main(String args) throws IOException / TODO Auto-generated method stubString fileName = "d:xxx.txt"File file = new File(fileName);byte b=newbyte(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 .edu.fileRWTester;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;publicclass FileRWTest /* * param args * throws IOException */publicstaticvoid 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版5601002 Java开发实战第1版8201003C+程序设计指南第3版3621004第1版12801005 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 .edu.Input.tester;import java.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;import .edu.Input.clas.Book;publicclass InputTester /* * param args * throws IOException */publicstaticvoid 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", "E3.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在包中,Mobile、Mp3Player、Product在包.data中,Mobile、Mp3Player是Product类的子类, Product类实现Seralizable接口。根本要求:1在Store类中用ObjectOutputStream类的对象把Mobile、Mp3Player类对象输出到文件“中。2在Store类中用ObjectInputStream类的对象从文件“输入数据并将其输出。实验设计:在product中重写writeObject和raedObject函数,并在主函数中调用writeObject和raedObject函数对文件读写。实验结果:写入文件的从文件中读出的:代码:Product中的writeObject和raedObject函数:privatevoidwriteObject(ObjectOutputStream oos) throws IOException oos.defaultWriteObject();oos.writeBytes(getName();oos.writeFloat(getPrice();privatevoid readObject(ObjectInputStream ois) throws IOException,ClassNotFoundException ois.defaultReadObject();主函数:package .edu.nwsuaf.jp.p4;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import .edu.nwsuaf.jp.p4.data.Mobile;import .edu.nwsuaf.jp.p4.data.Mp3Player;publicclass Store /* * param args */publicstaticvoid 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.out.println(ois.readObject();ois.close(); catch (Exception e) e.printStackTrace();3、 实验总结: 通过本次试验,掌握了输入输出流的简单用法,但运用不是很熟练,不看书很难编写下去。学习使用对象流,但对重写writeObject和raedObject函数掌握不是很好,还未做到熟练应用。 好的一点是,对toString函数可以熟练地掌握,并且能够自己发现错误,找到错误,并改正错误,虽然编写的很辛苦,但还是蛮有收获感的!