java实验9

上传人:第*** 文档编号:34251591 上传时间:2018-02-22 格式:DOC 页数:7 大小:102KB
返回 下载 相关 举报
java实验9_第1页
第1页 / 共7页
java实验9_第2页
第2页 / 共7页
java实验9_第3页
第3页 / 共7页
java实验9_第4页
第4页 / 共7页
java实验9_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《java实验9》由会员分享,可在线阅读,更多相关《java实验9(7页珍藏版)》请在金锄头文库上搜索。

1、山 西 大 学 计 算 机 与 信 息 技 术 学 院实 验 报 告姓 名 学 号 专业班级课程名称 Java实验 实验日期 2012. 4.4成 绩 指导教师 批改日期实 验 名 称 实 验 9 Java 的输入输出流一 实 验 目 的 :1. 理解 I/O 流的概念,掌握其分类2. 掌握文本文件读写、二进制文件读写 实验环境JDK1.4 以上版本, Eclipse 集成开发环境 实验内容 1. 分别使用 FileWriter 和 BufferedWriter 往文件中写入 10 万个随机数,比较用时的多少?(用时采用方法 System.currentTimeMillis())求时间差;使用

2、:FileWriterimport java.io.*;import java.util.*;public class gaopublic static void main(Stringargs)throws IOExceptionFileWriter output=new FileWriter(temp.txt); Random a = new Random();System.out.println(输入10万个随机数: );System.out.println(开始执行前时间: );System.out.print(System.currentTimeMillis();for(int i=

3、0;i0)c=(char)(in.readByte();if(c=A)count+;long estimatedTime = System.nanoTime() - startTime;System.out.println(estimatedTime);System.out.println(count);5.编写一个 Java 应用程序,给当前的类所在的文件 ReadExample.java 添加行号拷贝到另一个文件 temp.txt。import java.io.*;import java.util.*;public class ReadExample public static void

4、main(String args) throws IOException FileInputStream in = new FileInputStream(srcReadExampleReadExample.java);FileWriter file = new FileWriter(temp.txt);FileReader fr =new FileReader(srcReadExampleReadExample.java);BufferedWriter out = new BufferedWriter(file);BufferedReader read = new BufferedReade

5、r(fr);String a;int count = 1;out.write(0);out.write(1);out.write( );while(a = read.readLine() != null)count+;out.write(a);out.write(rn);out.write(0 + (count / 10);out.write(0 + (count % 10);out.write( );in.close();out.close(); 6. P54719.5(将对象和数组存储在文件中)编写一个程序,向一个名为 Exercise19_5.dat 的文件中存储一个含 5 个 int

6、值 1,2,3,4,5 的数组,存储一个表示当前时间的 Data 对象,存储一个 double 值 5.5。import java.io.*;public class hehepublic static void main(Stringargs) throws FileNotFoundException, IOException, ClassNotFoundExceptionint numbers=1,2,3,4,5;ObjectOutputStream output=new ObjectOutputStream(new FileOutputStream(Exercise19_5.dat);o

7、utput.writeDouble(5.5);output.writeObject(numbers);output.writeObject(new java.util.Date();output.close();ObjectInputStream input=new ObjectInputStream(new FileInputStream(Exercise19_5.dat);double a=input.readDouble();intb=(int)(input.readObject();java.util.Date date=(java.util.Date)(input.readObjec

8、t();for(int i=0;ib.length;i+)System.out.print(bi+ );System.out.println();System.out.println(a+ +date);输出结果为:1 2 3 4 5 5.5 Tue Apr 24 21:44:07 CST 20127. P548 19.12(组合文件)编写一个工具程序,使它能够用下面的命令,将文件组合在一起构成一个新文件:java Exercise19_12 SourceFile1SourceFilen TargetFile这个命令将 SourceFile1,SourceFile2., SourceFilen

9、 合并为 TargetFile。import java.io.*;public class gao public static void main(String args) throws IOException if(args.length != 3)System.out.println(Usage : java Copy sourceFile1 sourceFile2 targetFile );System.exit(0);File file1 = new File(args0);if(!file1.exists()System.out.println(sourceFile1 file +a

10、rgs0+ not exist);System.exit(0);File file2 = new File(args1);if(!file2.exists()System.out.println(sourceFile2 file +args1+ not exist);System.exit(0);File targetFile = new File(args2);if(targetFile.exists()System.out.println(targetFile file +args2+ already exist);System.exit(0);BufferedInputStream in

11、put = new BufferedInputStream(new FileInputStream(file1);BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(targetFile,true);int i;int count = 0;while(i = input.read() != -1)output.write(byte)i);count+;input = new BufferedInputStream(new FileInputStream(file2);while(i = input.read() != -1)output.write(byte)i);count+;input.close();output.close();System.out.println(count+ bytes copied );

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号