实验五 输入输出流_第1页
实验五 输入输出流_第2页
实验五 输入输出流_第3页
实验五 输入输出流_第4页
实验五 输入输出流_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验五 输入输出流一、实验目的1. 掌握文件的使用2. 掌握字节I/O流、字符类I/O流、缓冲流、数据流等常用流的使用。3. 熟悉对象流、RandomAccessFile类的使用。二、实验内容1. 利用各种常用IO流的处理方法,编写Java应用程序。三、实验步骤与要求第1题 文件加密编写一个Java应用程序,将已存在的扩展名为.txt的文本文件加密后存入另一个文本文件中。package four;import java.io.*;public class file public static void main(String args) File readFile =new File(&quo

2、t;Student.txt"), writeFile =new File("Hello.txt");String s="欢迎来到北京 welcome"char b=s.toCharArray();tryFileWriter out=new FileWriter(readFile,true);out.write(b);out.close();FileWriter tofile=new FileWriter ("hello.txt");for(int i=0;i<b.length;i+)bi=(char)(bi'w

3、');String secret=new String(b);System.out.println("原文:"+s);System.out.println("密文:"+secret);tofile.write(secret);tofile.close(); FileReaderinOne=new FileReader("Hello.txt"); int t=0; System.out.println("存入后:"); while(t=inOne.read(b,0,2)!=-1) String str=new

4、 String(b,0,2); System.out.println(str); inOne.close();catch (IOException e)System.out.println(e); 第2题 读写基本类型数据编写一个Java应用程序,将若干基本数据写入到一个文件,然后再按顺序读出。package five;/*第2题 读写基本类型数据编写一个Java应用程序,将若干基本数据写入到一个文件,然后再按顺序读出。*/import java.io.*;public class read public static void main(String args) try FileOutput

5、Stream os=new FileOutputStream("data.txt"); DataOutputStream outdata=new DataOutputStream(os); outdata.writeInt(99); outdata.writeShort(23); outdata.writeDouble(23.34535342132); outdata.writeFloat(3.123221312f); outdata.writeLong(2132445); outdata.writeChar('O'); outdata.writeByte(

6、2); outdata.writeBoolean(true); catch(IOException e) System.out.println( "int:" );try FileInputStream ns=new FileInputStream("data.txt"); DataInputStream intdata=new DataInputStream(ns); System.out.println( "int:" +intdata.readInt(); System.out.println( "short:&quo

7、t; +intdata.readShort(); System.out.println( "double:" +intdata.readDouble(); System.out.println( "float:" +intdata.readFloat(); System.out.println( "long:" +intdata.readLong(); System.out.println( "char:" +intdata.readChar(); System.out.println( "byte:&q

8、uot; +intdata.readByte(); System.out.println( "boolean:" +intdata.readBoolean();catch(IOException e)第3题 字符输入/输出流以及缓冲流的用法编写一个Java应用程序,给已存在的文本文件添加行号。要求该文本文件事先用文本编辑完毕,保存在C:code目录中,命名为Hello.txt.实验提示:可将BufferedWriter流和FileWriter流连接在一起,然后使用BufferedWriter流将数据写到目的地。BufferedReader对象调用readLine()方法可读

9、取文件的一行,BufferedWriter对象调用newLine()方法可向文件写入回行/package six;import java.io.*;public class code public static void main(String args)File readFile =new File ("Student.txt"),writeFile=new File("C:codehello.txt");if(!writeFile.getParentFile().exists() writeFile.mkdir(); /char b="欢迎

10、来到北京welcomenwelcome".toCharArray();try/FileWriter t=new FileWriter(readFile,true); /t.write(b); / t.close();FileReader inOne=new FileReader("Student.txt");BufferedReader inTwo=new BufferedReader(inOne);FileWriter tofile=new FileWriter("hello.txt");BufferedWriter out=new Buff

11、eredWriter(tofile);String s=null;int i=0;while(s=inTwo.readLine()!=null)i+;out.write(i+""+s);out.newLine();out.flush();out.close();tofile.close();inOne=new FileReader("hello.txt");inTwo=new BufferedReader(inOne);while(s=inTwo.readLine()!=null)System.out.println(s);inOne.close();i

12、nTwo.close();catch(IOException e)System.out.println(e);四、实验结果编写源程序并上机调试通过,根据实验过程填写实验报告,并上交源程序代码。 提交电子版实验报告要求: 1、源代码要规范、有详细的注释;每题以文件夹管理各个Java源文件;2、运行结果截图 +心得体会(实验报告)。打包命名:“学号姓名java实验几”: 如“2012085111小明java实验一”下周上课前交给学委选做题1. 对象流的使用Calendar类的用法参照书7.2)编写一个Java应用程序,将一个Calendar对象写入文件,然后顺序读出该对象,并验证读出的对象是原始对象的克隆。实验提示:使用对象流写入或读入对象时,要保证对象是序列化的。Calendar类实现了Serializable接口,其中接口中的方法对程序是不可见的,因此不需的在类中实现额外方法,Calendar类的对象是序列化对象。当把一个序列化对象写入到对象输出流时,JVM就会Serializable接口中的方法。使用对象流很容易获取一个序列化对象的克隆,只需将该对象写入到对象输出流,然后用对象输入流读回的对象就是原对象的一个克隆。2. RandomAccessFile类的使

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论