ImageVerifierCode 换一换
格式:DOCX , 页数:17 ,大小:72.39KB ,
资源ID:10280532      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/10280532.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(实验七.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

实验七.docx

1、实验七计算机科学与技术系 实 验 报 告 专业名称 计算机科学与技术 课程名称 JAVA语言程序设计 项目名称 JAVA语言的编程环境和开发工具 班 级 学 号 姓 名 实验日期 2016/3/7 实验七 文件管理与流式文件1、 实验性质:验证型2、 实验学时:课后完成一、实验目的1熟悉用文件File类创建、删除、查看文件或目录。2字节流、字符流、缓冲流、随机流等流式文件的创建,读写操作。3用字符流和缓冲流从键盘接受字符串的方法。二、实验内容1先运行该程序。源文件是projec3Ex7_1.java。然后按【思考问题】分析程序。import java.io.*; public class Ex

2、7_1 public static void main (String args) throws Exception File Mypath; FileInputStream Fin=null; FileOutputStream Fout=null; DataInputStream Din=null; DataOutputStream Dout=null; PrintWriter PWout=null; RandomAccessFile RAread=null; int x=0; Mypath=new File (f:aaaa); if(!Mypath.exists() System.out

3、.println(创建新目录);Mypath.mkdir(); else System.out .println(目录已存在); File Myfile1=new File (Mypath,myfile1.txt); File Myfile2=new File (Mypath,myfile2.txt); File Myfile3=new File (Mypath,myfile3.txt); try Fout=new FileOutputStream (Myfile1); Fin=new FileInputStream (Myfile1); Dout=new DataOutputStream(n

4、ew FileOutputStream (Myfile2); Din=new DataInputStream(new FileInputStream (Myfile2); PWout=new PrintWriter(new FileWriter(Myfile3); RAread=new RandomAccessFile(Myfile3,r); catch(FileNotFoundException e) System.out .println (文件未找到+e.toString(); String str; int num1; BufferedReader buf; buf=new Buffe

5、redReader(new InputStreamReader(System.in); System.out.print(请输入一个小于255整型数:); while(!(str=buf.readLine().equalsIgnoreCase(q) System.out.print(请输入另一个小于255整型数,按Q结束:); num1=Integer.parseInt(str); Fout.write(num1); Fout.close(); System.out.println(你刚输入的数据是:); while( x=Fin.read ()!=-1) System.out.println

6、(x); Fin.close(); System.out.print(请输入int范围内整型数:); while(!(str=buf.readLine().equalsIgnoreCase(q) ) System.out.print(请输入另一个整型数, 按Q结束:); num1=Integer.parseInt(str); Dout.writeInt(num1); Dout.close(); int leng=Din.available()/4; int xxx=0; while(xxxleng) xxx+; x=Din.readInt(); System.out.println(x); D

7、in.close(); System.out.print(请输入第一个字符串:); while(str=buf.readLine()!=null ) System.out.print(请输入另一个字符串,按Ctrl+Z结束:); PWout.println(str); PWout.close(); RAread.seek(0); while(RAread.getFilePointer()255、值0的情况进行观察;并且对照第二种情况输入同样数据,你得出什么结论?答:FileInputStream和FileOutputStream是字节流类型,数值的范围是:0到2555 对第二种流式文件判断文件

8、占用字节的长度用available()方法,而此处用int leng=Din.available()/4;为什么除以4?答:返回下一次对此输入流调用的方法可以不受阻塞的从此输入流读取(或跳过)的估计剩余字节数2按照第1题的内容,三种数据写到三个文件中,写入后然后读出校验。把所有其它流式文件全部改用随机流式文件来实现。import java.io.*;publicclass Ex7_2 publicstaticvoid main(String args)throws IOException File Mypath; RandomAccessFile Fin = null; PrintWriter

9、 Fout = null; RandomAccessFile Din = null; PrintWriter Dout = null; PrintWriter PWout = null; RandomAccessFile RAread = null; int x= 0; Mypath = new File(f:aaa); if(!Mypath.exists() System.out.println(创建新目录); Mypath.mkdir();/创建目录 else System.out.println(目录已存在); File Myfile1 = new File(Mypath,myfile1

10、.txt); File Myfile2 = new File(Mypath,myfile2.txt); File Myfile3 = new File(Mypath,myfile3.txt); try Fout = new PrintWriter(new FileWriter(Myfile1); Fin = new RandomAccessFile(Myfile1,r); Dout = new PrintWriter(new FileWriter(Myfile2); Din = new RandomAccessFile(Myfile2,r); PWout=new PrintWriter(new

11、 FileWriter(Myfile3); RAread=new RandomAccessFile(Myfile3,r); catch(FileNotFoundException e) System.out.println(文件未找到+e.toString(); String str; int num1; BufferedReader buf; buf = new BufferedReader(new InputStreamReader(System.in); System.out.println(请输入一个小于255整型数:); while(!(str = buf.readLine().eq

12、ualsIgnoreCase(q)/以q或Q System.out.print(请输入另一个小于255整型数:); num1 = Integer.parseInt(str); Fout.write(num1); Fout.close(); System.out.println(你刚才输入的数据是:); while(x = Fin.read()!=-1) System.out.println(x); Fin.close(); System.out.print(请输入int范围内整型数:); while(!(str = buf.readLine().equalsIgnoreCase(q) Syst

13、em.out.print(请输入另一个整型数); num1 = Integer.parseInt(str); Dout.write(num1); Dout.close(); int leng = (int) Din.length(); int xxx = 0; while(xxxleng) xxx+; x = Din.read(); System.out.println(x); Din.close(); System.out.print(请输入第一个字符串:); while(str=buf.readLine()!=null ) System.out.print(请输入另一个字符串,按Ctrl+

14、Z结束:); PWout.println(str); PWout.close(); /seek(long pos)设置此文件开头测量到的文件指针偏移量, /在该位置发生下一个读取或写入操作。 /偏移量的设置可能会超出文件末尾。偏移量的设置超出文件末尾不会改变文件的长度。 /只有在偏移量的设置超出文件末尾的情况下对文件进行写入才会更改其长度。 RAread.seek(0); while(RAread.getFilePointer()RAread.length() System.out.println(RAread.readLine(); RAread.close(); System.out.pr

15、intln (完成); /main结束/class结束【思考问题】1 注意创建随机文件对象对文件原有内容是清除还是保留?这一点是与其它流式文件很明显的特点,你怎样验证它?答:清除,往同一个文件里写东西。2 修改程序要求每次重新运行不覆盖原内容,新的数据填加在文件尾,然后读出校验。import java.io.*;publicclass Ex7_2 publicstaticvoid main(String args)throws IOException File Mypath; RandomAccessFile Fin = null; PrintWriter Fout = null; Rando

16、mAccessFile Din = null; PrintWriter Dout = null; PrintWriter PWout = null; RandomAccessFile RAread = null; int x= 0; Mypath = new File(f:aaa); if(!Mypath.exists() System.out.println(创建新目录); Mypath.mkdir();/创建目录 else System.out.println(目录已存在); File Myfile1 = new File(Mypath,myfile1.txt); File Myfile2

17、 = new File(Mypath,myfile2.txt); File Myfile3 = new File(Mypath,myfile3.txt); try Fout = new PrintWriter(new FileWriter(Myfile1); Fin = new RandomAccessFile(Myfile1,r); Dout = new PrintWriter(new FileWriter(Myfile2); Din = new RandomAccessFile(Myfile2,r); PWout=new PrintWriter(new FileWriter(Myfile3

18、); RAread=new RandomAccessFile(Myfile3,r); catch(FileNotFoundException e) System.out.println(文件未找到+e.toString(); String str; int num1; BufferedReader buf; buf = new BufferedReader(new InputStreamReader(System.in); System.out.println(请输入一个小于255整型数:); while(!(str = buf.readLine().equalsIgnoreCase(q)/以

19、q或Q System.out.print(请输入另一个小于255整型数:); num1 = Integer.parseInt(str); Fout.write(num1); Fout.close(); System.out.println(你刚才输入的数据是:); while(x = Fin.read()!=-1) System.out.println(x); Fin.close(); System.out.print(请输入int范围内整型数:); while(!(str = buf.readLine().equalsIgnoreCase(q) System.out.print(请输入另一个

20、整型数); num1 = Integer.parseInt(str); Dout.write(num1); Dout.close(); /返回下一次对此输入流调用的方法可以不受阻塞地从此输入流读取(或跳过)的估计剩余字节数。 int leng = (int) Din.length(); int xxx = 0; while(xxxleng) xxx+; x = Din.read(); System.out.println(x); Din.close(); System.out.print(请输入第一个字符串:); while(str=buf.readLine()!=null ) System.

21、out.print(请输入另一个字符串,按Ctrl+Z结束:); PWout.println(str); PWout.close(); /seek(long pos)设置此文件开头测量到的文件指针偏移量, /在该位置发生下一个读取或写入操作。 /偏移量的设置可能会超出文件末尾。偏移量的设置超出文件末尾不会改变文件的长度。 /只有在偏移量的设置超出文件末尾的情况下对文件进行写入才会更改其长度。 RAread.seek(0); while(RAread.getFilePointer()RAread.length() System.out.println(RAread.readLine(); RAr

22、ead.close(); System.out.print(请输入第一个字符串:); while(str=buf.readLine()!=null ) System.out.print(请输入另一个字符串,按Ctrl+Z结束:); PWout.println(str); PWout.close(); RAread.seek(RAread.length(); while(RAread.getFilePointer()RAread.length() System.out.println(RAread.readLine(); RAread.close(); System.out.println (完

23、成); /main结束/class结束3 修改程序如果只创建一个文件,三种键盘输入内容都放在同一个文件里,并读出校验。import java.io.*;publicclass Ex7_2 publicstaticvoid main(String args)throws IOException File Mypath; RandomAccessFile Fin = null; PrintWriter Fout = null; RandomAccessFile Din = null; PrintWriter Dout = null; PrintWriter PWout = null; Random

24、AccessFile RAread = null; int x= 0; Mypath = new File(f:aaa); if(!Mypath.exists() System.out.println(创建新目录); Mypath.mkdir();/创建目录 else System.out.println(目录已存在); File Myfile1 = new File(Mypath,myfile1.txt); File Myfile2 = new File(Mypath,myfile1.txt); File Myfile3 = new File(Mypath,myfile1.txt); try

25、 Fout = new PrintWriter(new FileWriter(Myfile1); Fin = new RandomAccessFile(Myfile1,rw); Dout = new PrintWriter(new FileWriter(Myfile2); Din = new RandomAccessFile(Myfile1,rw); PWout=new PrintWriter(new FileWriter(Myfile3); RAread=new RandomAccessFile(Myfile1,rw); catch(FileNotFoundException e) Syst

26、em.out.println(文件未找到+e.toString(); String str; int num1; BufferedReader buf; buf = new BufferedReader(new InputStreamReader(System.in); System.out.println(请输入一个小于255整型数:); while(!(str = buf.readLine().equalsIgnoreCase(q)/以q或Q System.out.print(请输入另一个小于255整型数:); num1 = Integer.parseInt(str); Fout.write(num1); Fout.close(); System.out.println(你刚才输入的数据是:); while(x = Fin.read()!=-1) System.out.println(x); Fin.close(); System.out.print(请输入int范围内整型数:); while(!(str = buf.readLine().equalsIgnoreCase(q) System.out.print(请输入另一个整型数); num1 = Integer.parseInt(str);

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1