您好,欢迎来到意榕旅游网。
搜索
您的当前位置:首页实验四 流式IO和异常处理

实验四 流式IO和异常处理

来源:意榕旅游网
实验四(Part 1) 流式输入输出处理

一、实验目的

1、理解流的概念,流的划分

2、 掌握文件描述,顺序处理,随机访问处理的方法;

3、 能够熟练的使用过滤流;掌握字符流处理的方法; 4、 理解对象串行化的概念和方法。

二、实验内容与要求

1、 仔细读下面的JAVA语言源程序,自己给出程序的运行结果,理解File类的使用。 import java.io.*; import java.awt.*;

import java.awt.event.*;

public class Ch71 extends Frame { int n;

String dirname=\"/jdk1.4\"; File file=new File(dirname); String[] flist=new String[100]; Choice listFile=new Choice(); TextField tf=new TextField();

public Ch71(){

setLayout(new BorderLayout(10,10)); flist=file.list();

for (n=0;nadd(\"North\ add(\"South\

setSize(250,200);

setVisible(true);

listFile.addItemListener(new koListener()); addWindowListener(new koWindowListener()); }

public static void main(String args[]) {

new Ch71(); }

class koListener implements ItemListener {

public void itemStateChanged(ItemEvent e) { String ko=(String)e.getItem(); for (n=0;nif (ko==flist[n]) { File f=new File(file+System.getProperty(\"file.separator\")+ko); if (f.isFile()==true)

tf.setText(ko+\"是个文件!\"); if (f.isDirectory()==true) tf.setText(ko+\"是个目录!\"); } } }

class koWindowListener extends WindowAdapter { public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } }

}

2、仔细读下面的JAVA语言源程序,自己给出程序的运行结果 import java.io.*;

public class testRandom2{

public static void main(String args[]){

try{

RandomAccessFile rf=new RandomAccessFile(\"rtest.dat\for(int i = 0; i < 10; i++){ rf.writeInt(i);

rf.writeDouble(i*1.414); }

rf.close();

rf = new RandomAccessFile(\"rtest.dat\rf.seek(5*(4+8)); rf.writeInt(47);

rf.writeDouble(47.00021); rf.close();

rf = new RandomAccessFile(\"rtest.dat\for(int i = 0; i < 10; i++){

;

System.out.println(\"Value \" + rf.readInt() + \": \" + rf.readDouble()); }

rf.close();

}catch(IOException e){

System.out.println(e.toString()); } } }

3、定义一个类实现银行帐户的概念,包括的变量有\"帐号\"和\"存款余额\",包括的方法有\"存款\"、\"取款\"、\"查询余额\"、”显示帐号”以及存储功能。定义主类,创建帐户类的对象,并完

成相应操作,要求能存储储户历史信息。

三、思考题

1、InputStream和OutputStream的基本方法是什么? 2、 DataOutputStream类的常用方法。 3、 Ascii编码和Unicode编码的区别? 5.字符流和字节流的区别是什么? 6.对文件进行读写操作的过程是什么?

四、实验报告

1、实验完成后,检查是否达到试验目的。

2、编写实验报告(打印或手写均可),在下一次上实验课时提交。

实验四(Part 2) 异常处理

一、实验目的

1、理解什么是异常

2、掌握java的异常处理机制和方法

二、实验内容与要求

1、仔细读下面的JAVA语言源程序,自己给出程序的运行结果,并上机验证。 mport java.io.*; public class Ch61 {

public static void main(String args[])throws IOException { int[] ko=new int[15];

int n,a; String x;

BufferedReader keyin=new BufferedReader(new InputStreamReader(System.in)); System.out.print(\"Enter an integer:\"); x=keyin.readLine(); n=Integer.parseInt(x); try {

a=110/n; ko[15]=100;

System.out.println(\"此描述无法执行!\"); } catch(ArithmeticException e){

System.out.println(\"除数为0的错误\"); } catch(ArrayIndexOutOfBoundsException f) {

System.out.println(\"数组索引值大于数组长度的错误!\");

}

System.out.println(\"执行完catch的描述!!!\"); }

}

2.阅读下面的程序,自己给出程序的运行结果,并上机验证。 import java.io.*; public class Ch62{ public Ch62(){ try{

int a[] = new int[2];

a[4] = 3;

System.out.println(\"After handling exception return here?\"); }catch(IndexOutOfBoundsException e){

System.err.println(\"exception msg:\" + e.getMessage()); System.err.println(\"exception string:\" + e.toString()); e.printStackTrace(); }finally{

System.out.println(\"-------------------\"); System.out.println(\"finally\"); }

System.out.println(\"No exception?\"); }

public static void main(String args[]){ new Ch62 (); } }

三、思考题

1. 下面程序在编译时会出现什么错误? import java.io.*;

class ExceptionDemo1{

public static void main(String args[]){

FileInputStream fis = new FileInputStream(“text.dat”); int b;

while(b=fis.read())!=-1 { System.out.print(b); }

fis.close(); }}

2.异常分为哪两类?

3.如果申明int a[]=new int[-10];应属于那类异常 4.比较throw和throws两者的用法上的区别。

5.本实验的实验内容2中,finally程序块的作用是什么?

四、实验报告

1、实验完成后,检查是否达到试验目的。 2、编写实验报告(打印或手写均可),在下一次上实验课时提交。

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- yrrf.cn 版权所有 赣ICP备2024042794号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务