1. finally 子句的作用
当一个异常被抛出时,程序的执行流程就不再是连续的了,会跳过某些语句,甚至会由于没有与之匹配的catch 子句而过早地返回,结束程序的运行。为了确保一段代码不管发生什么异常都能被执行,可以使用finally 子句。每个try 语句至少都要有一个与之相配的catch 或finally 子句。从一个方法中返回到调用它的另外一个方法,可以通过return 语句或通过一个没有被捕获的异常,但finally 子句总是在返回前被执行。
2. 创建使用finally 子句的程序文件LX4_12.java。
(1)程序功能:在类TestFinally 的mathodA 方法中使用try 子句捕获异常,使用finally 子句处理异常。在类TestFinally 的main 方法中使用try 子句捕获mathodA 异常,
class TestFinally{
static void mathodA() { try {
System.out.println(\抛出一个异常\throw new RuntimeException(); }finally {
System.out.println(\执行 mathodA 的 finally\}
static void mathodB() { try{
System.out.println(\正常返回\return; }finally {
System.out.println(\执行 mathodB 的 finally\}
public static void main(String args[]) { try {
mathodA();
} catch (Exception e) { mathodB(); } } }
(3)编译并运行该程序,结果如图8.9 所示。
图8.9
【完成实验项目】
1.自定义异常类MyException,该类继承自Exception类,类中只有含一个字符串参数msg的构造方法,构造方法中只有一条语句super(msg)——调用父类的构造方法。另外,编写自定义类person,类中只有两个私有的变量,一个是字符串类型的姓名,另一个是整型变量age;有两个公有方法void getAge()和setAge(int age),其中setAge(int age)的功能是把参数age的值加到类中的变量age中(但要求age>0,否则抛出自定义异常MyException类的对象),getAge()方法返回age的值。编写应用程序
45
实验十二 Java数据流编程
【开发语言及实现平台或实验环境】
Windows2000 或XP,JDK1.6与Eclipse
【实验目的】
1. 掌握不同类型的输入输出流类,标准数据流、文件流、数据输入输出流、对象流等。 2.理解Java 系统数据流的构成。 【实验要求】
1. 掌握Java的IO应用编程
【实验内容】
一、使用输入输出流类
1. 使用标准数据流的应用程序
标准数据流指在字符方式下(如DOS 提示符)程序与系统进行输入输出的方式,键盘和显示器屏幕是标准输入输出设备,数据输入的起点为键盘,数据输出的终点是屏幕。
(1)程序功能:将键盘上输入的字符在屏幕上显示出来。 (2)编写TestKeyInput.java 程序文件,源代码如下。 class TestKeyInput{
public static void main(String[] args) throws java.io.IOException {
byte buffer[]=new byte[10];
System.out.println(\从键盘输入不超过10 个字符,按回车键结束输入:\int count =System.in.read(buffer);//读取输入的字符并存放在缓冲区buffer 中 System.out.println(\保存在缓冲区buffer 中元素的个数为:\System.out.println(\中各元素的值为:\for (int i=0;i System.out.print(\在屏幕上显示buffer 元素的值 } System.out.println(); System.out.println(\输出buffer 字符元素:\System.out.write(buffer, 0, buffer.length); } } 2. 使用文件输入输出流的应用程序 (1)程序功能:将保存在本地机当前文件夹中的LX5_1.HTML 文本文件的内容在屏幕上显示出来,然后将其另存为LX5_1.txt 文件。 (2)编写TestCopy 程序文件,源代码如下。 import java.io.*; public class TestCopy{ public static void main(String[] args) throws IOException { FileReader in=new FileReader(\建立文件输入流 BufferedReader bin=new BufferedReader(in);//建立缓冲输入流 FileWriter out=new FileWriter(\建立文件输出流 String str; 46 while ((str=bin.readLine())!=null) {//将缓冲区内容通过循环方式逐行赋值给字符串str System.out.println(str);//在屏幕上显示字符串str out.write(str+\将字符串str 通过输出流写入LX5_1.txt 中 } in.close(); out.close(); } } (3) 编译并运行程序,结果如图9.4 所示。 图9.4 (4) 在当前文件夹中找到LX5_1.txt 文件并打开,可看到如图5.6 所示内容。再运行一次程序,看看会发生什么变化。 图9.5 3. 使用随机文件类的应用程序 使用文件输入类FileReader 只能将文件内容全部读入。如果要选择读入文件的内容,可使用随机文件类RandomAccessFile。 (1) 程序功能:建立数据流,通过指针有选择的读入文件内容。 (2) 编写LX5_5.java 程序文件,源代码如下。 import java.io.*; class TestRAndomAccessFile { public static void main(String args[]) { String str[]={\try { RandomAccessFile rf=new RandomAccessFile(\System.out.println(\文件指针位置为:\System.out.println(\文件的长度为:\rf.seek(rf.length()); System.out.println(\文件指针现在的位置为:\for (int i=0; i<3; i++) rf.writeChars(str[i]); // 字符串转为字节串添加到文件末尾 rf.seek(10); System.out.println(\选择显示的文件内容:\String s; while ((s=rf.readLine())!=null) System.out.println(s); rf.close(); }catch (FileNotFoundException fnoe) {} 47 catch (IOException ioe) {} } } (3)编译并运行程序,结果如图9.6 所示。 图9.6 4. 使用数据输入输出流与文件输入输出流类的应用程序 使用数据输出流DataOutputStream 和数据输入流DataInputStream 可以读取或写入任何Java 类型的数据,不用关心它们的实十际长度是多少字节。一般与文件输入流FileInputStream 和输出流类FileOutputStream 一起使用。 (1) 程序功能:将整型数据和字符串对象通过数据输出流写到文件中。将文件中的整型数据和字符串对象通过数据输出流读出,并在屏幕上显示文件中的内容。 import java.io.*; public class TestDataInputStream{ public static void main(String arg[]){ try{ //添加方式创建文件输出流 FileOutputStream fout = new FileOutputStream(\DataOutputStream dout = new DataOutputStream(fout); dout.writeInt(1); dout.writeChars(\罗马\dout.writeInt(2); dout.writeChars(\北京\dout.close(); }catch (IOException ioe){} try{ FileInputStream fin = new FileInputStream(\DataInputStream din = new DataInputStream(fin); int i = din.readInt(); while (i!=-1) {//输入流未结束时,输入流结束时i 为-1 System.out.print(i+\char ch ; while ((ch=din.readChar())!='\\n') //字符串未结束时 System.out.print(ch); System.out.println(); i = din.readInt(); } din.close(); }catch (IOException ioe){} } } (2) 编译并运行程序,结果如图9.7 所示。 48 图9.7 5. 使用对象输入输出流的应用程序 使用对象流可以直接写入或读取一个对象。由于一个类的对象包含多种信息,为了保证从对象流中能够读取到正确的对象,因此要求所有写入对象流的对象都必须是序列化的对象。一个类如果实现了Serializable 接口,那么这个类的对象就是序列化的对象。Serializable 接口没有方法,实现该接口的类不需要实现额外的方法。 (1) 程序功能:保存对象信息到文件,并将文件中的对象信息显示出来。 import java.io.*; public class TestObject implements Serializable{ //序列化接口 int bh=1;int nl=21; String xm; LX10_3(int bh,String xm,int nl) {//构造方法 this.bh = bh; this.xm = xm; this.nl = nl; } LX10_3(){//构造方法 this(0,\} void save(String fname) {//保存到文件中的方法 try{ FileOutputStream fout = new FileOutputStream(fname);//输出文件流 ObjectOutputStream out = new ObjectOutputStream(fout);//输出对象流 out.writeObject(this); //写入对象 out.close(); }catch (FileNotFoundException fe){} catch (IOException ioe){} } void display(String fname) {//显示文件中对象信息的方法 try{ FileInputStream fin = new FileInputStream(fname);//输入文件流 ObjectInputStream in = new ObjectInputStream(fin);//输入对象流 LX10_3 OO = (LX10_3)in.readObject(); //读取对象 System.out.println(\类名: \\ System.out.println(\in.close(); }catch (FileNotFoundException fe){} catch (IOException ioe){} catch (ClassNotFoundException ioe) {} } 49 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库java实验指导书(10)在线全文阅读。
相关推荐: