当前位置:文档之家› 实验11 输入输出流(答案)

实验11 输入输出流(答案)

实验11输入输出流一、实验目的(1)掌握Java I/O基本原理。

(2)掌握标准输入/输出流和Scanner类的基本使用方法。

(3)掌握FileInputStream、FileOutputStream抽象类的基本使用方法。

二、实验任务(1)阅读给定的Java Application程序,按要求回答问题并写出运行结果。

(2)按要求编写一个Java Application程序,并编译、运行这个程序。

三、实验内容1、从标准输入读取数据import java.io.*;import java.util.*;public class StdInput {public static void main(String[] args) throws Exception {1:System.out.println("input:");2:byte b[]=new byte[512];3:int count=System.in.read(b);4:System.out.println("count=:"+count);5:System.out.println("output:");6:for(int i=0;i<count;i++)7:{System.out.print(b[i]+" ");}} }(1)分析程序代码,写出运行结果。

(2)将编号为6:和7:的两行改成一行语句System.out.println(b);输出的结果如何?分析为什么?(3)如果将编号为2:的那一行代码改成double b[]=new double[512];那么修改其他的地方,完成正确输入和输出double类型的数组元素,请写出修改后的完整的运行代码和结果。

import java.io.*;import java.util.*;public class StdInput {public static void main(String[] args) throws Exception {System.out.println("input:");double b[]=new double[512];Scanner sin=new Scanner(System.in);//int count=System.in.read();//System.out.println("count=:"+count);System.out.println("output:");for(int i=0;i<b.length;i++){b[i]=sin.nextDouble();System.out.print(b[i]+" ");}} }2、编写程序实现如下操作过程:先在指定的当前目录下创建名“temp”的子目录,在“temp”目录下创建两个文件“temp1.txt”和“temp2.txt”,然后列表显示temp目录下的所有文件;接下来再重命名“temp1.txt”为“temp3.txt”,删除文件“temp2.txt”,再在temp目录下创建tp子目录,在“tp”目录下创建两个文件“temp4.txt”和“temp5.txt”,然后再次显示temp目录下的所有文件。

import java.io.*;public class FileDirectory {public static void main(String[] args) throws Exception{ File currentPath=new File(".");File tempPath=new File(currentPath,"temp");tempPath.mkdir();File temp1=new File(tempPath,"temp1.txt");temp1.createNewFile();File temp2=new File(tempPath,"temp2.txt");temp2.createNewFile();【代码1】//创建文件temp2.txtSystem.out.println("第一次的目录文件:");listsubdir(tempPath);File newf=new File(tempPath,"temp3.txt");temp1.renameTo(newf);temp2.delete();【代码2】//删除temp2.txt文件File tempPath1=new File(tempPath,"tp");tempPath1.mkdir();【代码3】//创建子目录tpFile temp4=new File(tempPath1,"temp4.txt");temp4.createNewFile();【代码4】//创建文件temp4.txtFile temp5=new File(tempPath1,"temp5.txt");temp5.createNewFile();System.out.println("更改后的目录文件:");listsubdir(tempPath);}//递归显示指定目录的内容方法static void listsubdir(File currentPath)throws Exception {String filenames[]=currentPath.list();for(int i=0;i<filenames.length;i++){File f=new File(currentPath,filenames[i]);if(f.isDirectory())【代码5】//判断f是否是目录{System.out.println("仍然是一个目录,进行递归调用"+f.getAbsolutePath());listsubdir(f);}else System.out.println(f.getName());}}}写出运行结果:3.将如下三组不同类型的数据利用DataInputStream和DataOutputStream写入文件,然后从文件中读出。

三组数据如下:{ 19.99, 9.99, 15.99,3.99, 4.99 }; { 12, 8, 13, 29, 50 };{ "Java T-shirt", "Java Mug", "Duke Juggling Dolls", "Java Pin","Java Key Chain"} import java.io.*;public class S2 {public static void main(String[] args) throws IOException { double prices []={19.99,9.99,8.97};int units []={12,8,9};String describle []={"java t-shirt","java mug","java coat"};DataOutputStream out = new DataOutputStream(newFileOutputStream("test.txt"));//创建数据输出流for (int i = 0; i < prices.length; i ++){ out.writeDouble(prices[i]);out.writeChar('\t');out.writeInt(units[i]);【代码1】//写入数量out.writeChar('\t');out.writeChars(describle[i]);out.writeChar('\n');}out.close();//关闭数据输出流DataInputStream in = new DataInputStream(new FileInputStream("test.txt"));【代码2】//创建数据输入流double price;int unit;String desc;double total = 0.0;try{//利用数据输入流读文件内容while (true){ price = in.readDouble();【代码3】//读出价格in.readChar(); // throws out the tabunit = in.readInt();in.readChar(); // throws out the tabdesc= in.readLine();System.out.println("你已经订购:" + unit + " 数量的" +desc + " 价格为:" + price);total = total + unit * price;}}catch (EOFException e)//捕获异常{e.printStackTrace();}System.out.println("总价格为: $" + total);in.close(); //关闭数据输入流}}4、将上题中的三组不同类型的数据利用对象串行化的方式写入文件,然后从文件中读出。

import java.io.*;class prices implements Serializable【代码2】//定义prices类{ double price;int unit;String product;prices(double pc,int un,String pt){price=pc;unit=un;product= pt;}}public class S1 {public static void main(String[] args) throws IOException, ClassNotFoundException {double pri []={19.99,9.99,8.97};int units []={12,8,9};String describle []={"java t-shirt","java mug","java coat"};for(int i=0;i<pri.length;i++){prices ps=new prices(pri[i],units[i],describle[i]);FileOutputStream fos=new FileOutputStream("f2.txt");ObjectOutputStream oout=new ObjectOutputStream(fos);//实例化输出对象ooutoout.writeObject(ps);【代码2】//写入对象oout.flush();ps=null;ObjectInputStream oin=new ObjectInputStream(new FileInputStream("f2.txt"));【代码3】//实例化输入对象oin ps=(prices)oin.readObject();oin.close();System.out.println("输出prices["+i+"]类的信息:");System.out.print("jiage:"+ps.price);System.out.print("\t");System.out.print("shuliang :"+ps.unit);System.out.print("\t");System.out.print("migncheng:"+ps.product);System.out.print("\n");}}}5、比较下面两段代码,执行之后有什么区别,说明原因。

相关主题