zoukankan      html  css  js  c++  java
  • 201771010134杨其菊《面向对象程序设计java》第九周学习总结

                                                                      第九周学习总结

     

    第一部分:理论知识

    异常、断言和调试、日志

    1.捕获异常:
        要捕获一个异常,必须设置try/catch语句块

        例如:

        try{

        }catch(ExceptionType e){

        }

        如果方法中的任何代码抛出一个在catch字句中没有声明的异常类型,那么这个方法就会立刻退出

        通常,应该捕获那些知道如何处理的异常,而将那些不知道怎么处理的异常继续传递

    2.捕获多个异常:
        例如:

        try{

        }catch(FileNotFoundException e){

        }catch(UnknownHostException e){

        }catch(IOException e){

        }

        异常对象可能包含与异常本身相关的信息。要想获得对象的更多信息,可以试着使用

        e.getMessage()得到详细的错误信息

        e.getClass().getName()得到异常对象的实际类型

        在Java SE 7中,同一个catch字句中可以捕获多个异常类型,例如:

        try{

        }catch(FileNotFoundException | UnknownHostException e){

        }

        

    3.常见的断言方式


      前置条件断言:代码执行之前必须具备的特性
      后置条件断言:代码执行之后必须具备的特性
      前后不变断言:代码执行前后不能变化的特性

     

    4.断言使用方式
      断言可以有两种形式
      1.assert Expression1
      2.assert Expression1:Expression2

      其中Expression1应该总是一个布尔值,Expression2是断言失败时输出的失败消息的字符串。
    如果Expression1为假,则抛出一个 AssertionError,【这是一个错误,而不是一个异常】,也就是说是一个【不可控制异常】(unchecked Exception),        AssertionError由于是错误,所以可以不捕获,但不推荐这样做,因为那样会使你的系统进入不稳定状态。

    5.分析堆栈轨迹元素:
        堆栈轨迹是一个方法调用过程的列表,它包含了程序执行过程中方法调用的特定位置

        可以使用Throwable类的printStackTrace方法访问堆栈轨迹的文本描述信息

        Throwable t = new Throwable();

        StringWriter out = new StringWriter();

        t.printStackTrace(new PrintWriter(out));

        String description = out.toString();

        也可以使用getStackTrace方法,它会得到StackTraceElement对象的一个数组

        StackTraceElement类含有能够获得文件名和当前执行代码行号的方法,同时,还含有能够获得类名和方法名的方法

        静态的Thread.getAllStackTrace方法,它可以产生所有线程的堆栈轨迹,例如:

        Map<Thread,StackTraceElement[]> map = Thread.getAllStackTrace();

        for(Thread t : map.keySet()){

            StackTraceElement[] f = map.get(t);

        }

    6.java.lang.Throwable 1.0:
        Throwable(Throwable cause) 1.4

        Throwable(String message,Throwable cause) 1.4

        用给定的原因构造一个Throwable对象

        Throwable initCause(Throwable cause) 1.4

        将这个对象设置为原因,如果这个对象已经被设置为原因,则抛出一个异常,返回this引用

        Throwable getCause() 1.4

        获得设置为这个对象的原因的异常对象,如果没有,返回null

        StackTraceElement[] getStackTrace() 1.4

        获得构造这个对象时调用堆栈的跟踪

        void addSuppressed(Throwable t) 7

        为这个异常增加一个抑制异常

        Throwable[] getSuppressed() 7

        得到这个异常的所有“抑制”异常

    7.java.lang.Exception 1.0:
        Exception(Throwable cause) 1.4

        Exception(String message,Throwable cause)

        用给定的原因构造一个异常对象

    8.java.lang.RuntimeException 1.0:
        RuntimeException(Throwable cause) 1.4

        RuntimeException(String message,Throwable cause) 1.4

        用给定的原因构造一个RuntimeException对象

    9.java.lang.StackTraceElement 1.4:
        String getFileName()

        返回这个元素运行时对应的源文件名。

        int getLineNumber()

        返回这个元素运行时对应的源文件行数

        String getClassName()

        返回这个元素运行时对应的类完全限定名

        String getMethodName()

        返回这个元素运行时对应的方法名

        boolean isNativeMethod()

        如果这个元素运行时在一个本地方法中,则返回true

        String toString()

        如果存在的话,返回一个包含类名、方法名、文件名和行数的格式化字符串

    另:. java随机函数用法Random - leihupqrst - 博客园  file:///G:/java%E9%9A%8F%E6%9C%BA%E5%87%BD%E6%95%B0%E7%94%A8%E6%B3%95Random%20-%20leihupqrst%20-%20%E5%8D%9A%E5%AE%A2%E5%9B%AD.html

    第二部分:实验部分

    1、实验目的与要求

    (1) 掌握java异常处理技术;

    (2) 了解断言的用法;

    (3) 了解日志的用途;

    (4) 掌握程序基础调试技巧;

    2、实验内容和步骤

    实验1:用命令行与IDE两种环境下编辑调试运行源程序ExceptionDemo1、ExceptionDemo2,结合程序运行结果理解程序,掌握未检查异常和已检查异常的区别。

    //异常示例1

    public class ExceptionDemo1 {

    public static void main(String args[]) {

    int a = 0;

    System.out.println(5 / a);

    }

    }

    //异常示例2

    import java.io.*;

    public class ExceptionDemo2 {

    public static void main(String args[])

         {

              FileInputStream fis=new FileInputStream("text.txt");//JVM自动生成异常对象

              int b;

              while((b=fis.read())!=-1)

              {

                  System.out.print(b);

              }

              fis.close();

          }

    }

    (1)

    (2)

     1 //异常示例2
     2 import java.io.*;
     3 
     4 public class ExceptionDemo2 {
     5     public static void main(String args[]) throws Exception 
     6    {
     7         FileInputStream fis=new FileInputStream("text.txt");//JVM自动生成异常对象
     8         int b;
     9         while((b=fis.read())!=-1)
    10         {
    11             System.out.print(b);
    12         }
    13         fis.close();
    14     }
    15 }

    实验2 导入以下示例程序,测试程序并进行代码注释。

    测试程序1:

    l 在elipse IDE中编辑、编译、调试运行教材281页7-1,结合程序运行结果理解程序;

    l 在程序中相关代码处添加新知识的注释;

    l 掌握Throwable类的堆栈跟踪方法;

    源代码:

    package thesecond;
    
    import java.util.Scanner;
    
    /**
     * A program that displays a trace feature of a recursive method call.
     * @version 1.01 2004-05-10
     * @author Cay Horstmann
     */
    public class StackTraceTest
    {
       /**
        * Computes the factorial of a number
        * @param n a non-negative integer
        * @return n! = 1 * 2 * . . . * n
        */
       public static int factorial(int n)
       {
          System.out.println("factorial(" + n + "):");
          Throwable t = new Throwable();//在lang包中,不必写出;
          StackTraceElement[] frames = t.getStackTrace();//调用getStackTrace方法,得到StackTraceElement对象的一个数组;
          
        
          for (StackTraceElement f : frames)
             System.out.println(f);
          //递归算法;
        //每次return都是return栈顶那个计算得到的值,然后出栈,再把return得值作为已知条件来计算下个表达式
          int r;
          if (n <= 1) r = 1;
          else r = n * factorial(n - 1);//n!=n*(n-1)!;
          System.out.println("return " + r);
          return r;
       }
    
       public static void main(String[] args)
       {
          Scanner in = new Scanner(System.in);
          System.out.print("Enter n: ");
          int n = in.nextInt();
          factorial(n);
       }
    }

    截图:

    StackTrace就是当程序运行时一系列的函数调用的轨迹。

    测试程序2

    l Java语言的异常处理积极处理方法和消极处理两种方式

    l 下列两个简答程序范例给出了两种异常处理的代码格式。在elipse IDE中编辑、调试运行源程序ExceptionalTest.java,将程序中的text文件更换为身份证号.txt,要求将文件内容读入内容,并在控制台显示;

    l 掌握两种异常处理技术的特点。

    //积极处理方式  

    import java.io.*;

    class ExceptionTest {

    public static void main (string args[])

       {

           try{

           FileInputStream fis=new FileInputStream("text.txt");

           }

           catchFileNotFoundExcption e

         {   ……  }

    ……

        }

    }

    //消极处理方式

    import java.io.*;

    class ExceptionTest {

    public static void main (string args[]) throws  FileNotFoundExcption

         {

          FileInputStream fis=new FileInputStream("text.txt");

         }

    }


     

     1 package thesecond;
     2 //积极处理方式  
     3 import java.io.*;
     4 import java.io.BufferedReader;
     5 import java.io.FileReader;
     6 
     7 class ExceptionTest {
     8     public static void main (String args[])
     9  {
    10      try{
    11            File fis=new File("身份证号.txt");// 创建File类对象fis,传递一个本地文件的绝对路径
    12 
    13              FileReader f = new FileReader(fis);
    14              BufferedReader b= new BufferedReader(f);
    15              try {
    16                  String s, s2 = new String();
    17                  while ((s = b.readLine()) != null) {
    18                      s2 += s + "
     ";
    19                  }
    20                  b.close();
    21                  System.out.println(s2);
    22              }
    23              catch (IOException e) {
    24                  
    25             
    26                  e.printStackTrace();
    27              }
    28          } 
    29             catch (FileNotFoundException e) {
    30              // TODO Auto-generated catch block
    31              e.printStackTrace();
    32          }
    33 
    34       }
    35     }
    package thesecond;
    import java.io.*;
    import java.io.BufferedReader;
    import java.io.FileReader;
    
    //消极处理方式
    
    import java.io.*;
    class ExceptionTest {
      public static void main (String args[]) throws  IOException
         {
          File fis=new File("身份证号.txt");
          FileReader fr = new FileReader(fis);
          BufferedReader br = new BufferedReader(fr);
          String s, s2 = new String();
    
              while ((s = br.readLine()) != null) {
                  s2 += s + "
     ";
              }
              br.close();
              System.out.println(s2);
         }
    }

    实验3: 编程练习

    练习1

    编制一个程序,将身份证号.txt 中的信息读入到内存中;

    l 按姓名字典序输出人员信息;

    l 查询最大年龄的人员信息;

    l 查询最小年龄人员信息;

    输入你的年龄,查询身份证号.txt中年龄与你最近人的姓名、身份证号、年龄、性别和出生地;

    l 查询人员中是否有你的同乡;

    l 在以上程序适当位置加入异常捕获代码。

      1 import java.io.BufferedReader;
      2         import java.io.File;
      3         import java.io.FileInputStream;
      4         import java.io.FileNotFoundException;
      5         import java.io.IOException;
      6         import java.io.InputStreamReader;
      7         import java.util.ArrayList;
      8         import java.util.Arrays;
      9         import java.util.Collections;
     10         import java.util.Scanner;
     11 
     12 
     13 public class Search{
     14 
     15       private static ArrayList<Person> Personlist1;
     16        public static void main(String[] args) {
     17          
     18           Personlist1 = new ArrayList<>();
     19          
     20           Scanner scanner = new Scanner(System.in);
     21           File file = new File("E:\面向对象程序设计Java\实验\实验六\身份证号.txt");
     22    
     23                 try {
     24                      FileInputStream F = new FileInputStream(file);
     25                      BufferedReader in = new BufferedReader(new InputStreamReader(F));
     26                      String temp = null;
     27                      while ((temp = in.readLine()) != null) {
     28                         
     29                         Scanner linescanner = new Scanner(temp);
     30                         
     31                         linescanner.useDelimiter(" ");    
     32                         String name = linescanner.next();
     33                         String id = linescanner.next();
     34                         String sex = linescanner.next();
     35                         String age = linescanner.next();
     36                         String place =linescanner.nextLine();
     37                         Person Person = new Person();
     38                         Person.setname(name);
     39                         Person.setid(id);
     40                         Person.setsex(sex);
     41                         int a = Integer.parseInt(age);
     42                         Person.setage(a);
     43                         Person.setbirthplace(place);
     44                         Personlist1.add(Person);
     45 
     46                     }
     47                 } catch (FileNotFoundException e) {
     48                     System.out.println("查找不到信息");
     49                     e.printStackTrace();
     50                 } catch (IOException e) {
     51                     System.out.println("信息读取有误");
     52                     e.printStackTrace();
     53                 }
     54                 boolean isTrue = true;
     55                 while (isTrue) {
     56                     System.out.println("******************************************");
     57                     System.out.println("1:按姓名字典顺序输出信息;");
     58                     System.out.println("2:查询最大年龄与最小年龄人员信息;");
     59                     System.out.println("3:按省份找你的同乡;");
     60                     System.out.println("4:输入你的年龄,查询年龄与你最近人的信息;");
     61                     System.out.println("5:退出");
     62                     System.out.println("******************************************");
     63                     int type = scanner.nextInt();
     64                     switch (type) {
     65                     case 1:
     66                         Collections.sort(Personlist1);
     67                         System.out.println(Personlist1.toString());
     68                         break;
     69                     case 2:
     70                         
     71                         int max=0,min=100;int j,k1 = 0,k2=0;
     72                         for(int i=1;i<Personlist1.size();i++)
     73                         {
     74                             j=Personlist1.get(i).getage();
     75                            if(j>max)
     76                            {
     77                                max=j; 
     78                                k1=i;
     79                            }
     80                            if(j<min)
     81                            {
     82                                min=j; 
     83                                k2=i;
     84                            }
     85 
     86                         }  
     87                         System.out.println("年龄最大:"+Personlist1.get(k1));
     88                         System.out.println("年龄最小:"+Personlist1.get(k2));
     89                         break;
     90                     case 3:
     91                         System.out.println("place?");
     92                         String find = scanner.next();        
     93                         String place=find.substring(0,3);
     94                         String place2=find.substring(0,3);
     95                         for (int i = 0; i <Personlist1.size(); i++) 
     96                         {
     97                             if(Personlist1.get(i).getbirthplace().substring(1,4).equals(place)) 
     98                             {
     99                                 System.out.println("你的同乡:"+Personlist1.get(i));
    100                             }
    101                         } 
    102 
    103                         break;
    104                     case 4:
    105                         System.out.println("年龄:");
    106                         int yourage = scanner.nextInt();
    107                         int close=ageclose(yourage);
    108                         int d_value=yourage-Personlist1.get(close).getage();
    109                         System.out.println(""+Personlist1.get(close));
    110                   
    111                         break;
    112                     case 5:
    113                    isTrue = false;
    114                    System.out.println("再见!");
    115                         break;
    116                     default:
    117                         System.out.println("输入有误");
    118                     }
    119                 }
    120             }
    121             public static int ageclose(int age) {
    122                    int m=0;
    123                 int    max=53;
    124                 int d_value=0;
    125                 int k=0;
    126                 for (int i = 0; i < Personlist1.size(); i++)
    127                 {
    128                     d_value=Personlist1.get(i).getage()-age;
    129                     if(d_value<0) d_value=-d_value; 
    130                     if (d_value<max) 
    131                     {
    132                        max=d_value;
    133                        k=i;
    134                     }
    135 
    136                  }    return k;
    137                 
    138              }
    139 
    140    
    141 
    142  }
    143 
    144 
    145 
    146 
    147 
    148 
    149 
    150 
    151 //jiekouwenjiaan
    152 
    153 
    154 public class Person implements Comparable<Person> {
    155             private String name;
    156             private String id;
    157             private int age;
    158             private String sex;
    159             private String birthplace;
    160 
    161     public String getname() {
    162         return name;
    163         }
    164     public void setname(String name) {
    165         this.name = name;
    166     }
    167     public String getid() {
    168         return id;
    169     }
    170     public void setid(String id) {
    171         this.id= id;
    172     }
    173     public int getage() {
    174     
    175         return age;
    176     }
    177     public void setage(int age) {
    178         // int a = Integer.parseInt(age);
    179         this.age= age;
    180     }
    181     public String getsex() {
    182         return sex;
    183     }
    184     public void setsex(String sex) {
    185         this.sex= sex;
    186     }
    187     public String getbirthplace() {
    188         return birthplace;
    189     }
    190     public void setbirthplace(String birthplace) {
    191         this.birthplace= birthplace;
    192 }
    193 
    194     public int compareTo(Person o) {
    195         return this.name.compareTo(o.getname());
    196 
    197 }
    198 
    199     public String toString() {
    200         return  name+"	"+sex+"	"+age+"	"+id+"	";
    201 
    202 }
    203 
    204 
    205 
    206 }

     

    注:以下实验课后完成

    练习2

    l 编写一个计算器类,可以完成加、减、乘、除的操作;

    利用计算机类,设计一个小学生100以内数的四则运算练习程序,由计算机随机产生10道加减乘除练习题,学生输入答案,由程序检查答案是否正确,每道题正确计10分,错误不计分,10道题测试结束后给出测试总分;

    将程序中测试练习题及学生答题结果输出到文件,文件名为test.txt

     1 import java.util.*;
     2 import java.util.Scanner;
     3 
     4 public class Calculator {
     5     
     6     public static void main(String[] args) {
     7         // 用户的答案要从键盘输入,因此需要一个键盘输入流
     8         Scanner in = new Scanner(System.in);
     9         // 定义一个变量用来统计得分
    10         int sum = 0;
    11         // 通过循环生成10道题
    12         for (int i = 0; i < 10; i++) {
    13 
    14             // 随机生成两个10以内的随机数作为被除数和除数
    15             int a = (int) Math.round(Math.random() * 10);
    16             int b = (int) Math.round(Math.random() * 10);
    17             int c = (int) Math.round(Math.random() * 4);
    18             
    19         int s;
    20             
    21      switch(c)  {   
    22      
    23            case 1:
    24                  s=a+b;
    25                  System.out.println(a + "+" + b + "=");
    26                  break;
    27              case 2:
    28                   s=s=a-b;
    29                  System.out.println(a + "-" + b + "=");
    30                  break;
    31             case 3:
    32                  s=a*b;
    33                  System.out.println(a + "*" + b + "=");
    34                    break;   
    35             case 4:
    36                 
    37                  s=a/b;
    38                  System.out.println(a + "/" + b + "=");
    39                
    40                     break;
    41            
    42              }
    43     
    44      int s1 = in.nextInt();
    45         
    46         if (s1==s) {
    47             sum += 10;
    48             System.out.println("恭喜答案正确");
    49           }
    50         else {
    51             
    52             System.out.println("抱歉,答案错误");
    53             }
    54     }    
    55         //输出用户的成绩
    56         System.out.println("你的得分为"+sum);
    57 }
    58 
    59 }

    l 在以上程序适当位置加入异常捕获代码。

    实验4:断言、日志、程序调试技巧验证实验。

    实验程序1

    //断言程序示例

    public class AssertDemo {

        public static void main(String[] args) {        

            test1(-5);

            test2(-3);

        }

        

        private static void test1(int a){

            assert a > 0;

            System.out.println(a);

        }

        private static void test2(int a){

           assert a > 0 : "something goes wrong here, a cannot be less than 0";

            System.out.println(a);

        }

    }

    l 在elipse下调试程序AssertDemo,结合程序运行结果理解程序;

    //断言程序示例
    public class AssertDemo {
        public static void main(String[] args) {        
            test1(-5);
            test2(-3);
        }
        
        private static void test1(int a){
            assert a > 0;
            System.out.println(a);
        }
        private static void test2(int a){
           assert a > 0 : "something goes wrong here, a cannot be less than 0";
            System.out.println(a);
        }
    }

    l 注释语句test1(-5);后重新运行程序,结合程序运行结果理解程序;

     

    注释后运行:

    l 掌握断言的使用特点及用法。

    实验程序2:

    l 用JDK命令调试运行教材298-300页程序7-2,结合程序运行结果理解程序;

    l 并掌握Java日志系统的用途及用法。

      1 import java.awt.*;
      2 import java.awt.event.*;
      3 import java.io.*;
      4 import java.util.logging.*;
      5 import javax.swing.*;
      6 
      7 /**
      8  * A modification of the image viewer program that logs various events.
      9  * @version 1.03 2015-08-20
     10  * @author Cay Horstmann
     11  */
     12 public class LoggingImageViewer
     13 {
     14    public static void main(String[] args)
     15    {
     16       if (System.getProperty("java.util.logging.config.class") == null
     17             && System.getProperty("java.util.logging.config.file") == null)
     18       {
     19          try
     20          {
     21             Logger.getLogger("com.horstmann.corejava").setLevel(Level.ALL);
     22             final int LOG_ROTATION_COUNT = 10;
     23             Handler handler = new FileHandler("%h/LoggingImageViewer.log", 0, LOG_ROTATION_COUNT);
     24             Logger.getLogger("com.horstmann.corejava").addHandler(handler);
     25          }
     26          catch (IOException e)
     27          {
     28             Logger.getLogger("com.horstmann.corejava").log(Level.SEVERE,
     29                   "Can't create log file handler", e);
     30          }
     31       }
     32 
     33       EventQueue.invokeLater(() ->
     34             {
     35                Handler windowHandler = new WindowHandler();
     36                windowHandler.setLevel(Level.ALL);
     37                Logger.getLogger("com.horstmann.corejava").addHandler(windowHandler);
     38 
     39                JFrame frame = new ImageViewerFrame();
     40                frame.setTitle("LoggingImageViewer");
     41                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     42 
     43                Logger.getLogger("com.horstmann.corejava").fine("Showing frame");
     44                frame.setVisible(true);
     45             });
     46    }
     47 }
     48 
     49 /**
     50  * The frame that shows the image.
     51  */
     52 class ImageViewerFrame extends JFrame
     53 {
     54    private static final int DEFAULT_WIDTH = 300;
     55    private static final int DEFAULT_HEIGHT = 400;   
     56 
     57    private JLabel label;
     58    private static Logger logger = Logger.getLogger("com.horstmann.corejava");
     59 
     60    public ImageViewerFrame()
     61    {
     62       logger.entering("ImageViewerFrame", "<init>");      
     63       setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
     64 
     65       // set up menu bar
     66       JMenuBar menuBar = new JMenuBar();
     67       setJMenuBar(menuBar);
     68 
     69       JMenu menu = new JMenu("File");
     70       menuBar.add(menu);
     71 
     72       JMenuItem openItem = new JMenuItem("Open");
     73       menu.add(openItem);
     74       openItem.addActionListener(new FileOpenListener());
     75 
     76       JMenuItem exitItem = new JMenuItem("Exit");
     77       menu.add(exitItem);
     78       exitItem.addActionListener(new ActionListener()
     79          {
     80             public void actionPerformed(ActionEvent event)
     81             {
     82                logger.fine("Exiting.");
     83                System.exit(0);
     84             }
     85          });
     86 
     87       // use a label to display the images
     88       label = new JLabel();
     89       add(label);
     90       logger.exiting("ImageViewerFrame", "<init>");
     91    }
     92 
     93    private class FileOpenListener implements ActionListener
     94    {
     95       public void actionPerformed(ActionEvent event)
     96       {
     97          logger.entering("ImageViewerFrame.FileOpenListener", "actionPerformed", event);
     98 
     99          // set up file chooser
    100          JFileChooser chooser = new JFileChooser();
    101          chooser.setCurrentDirectory(new File("."));
    102 
    103          // accept all files ending with .gif
    104          chooser.setFileFilter(new javax.swing.filechooser.FileFilter()
    105             {
    106                public boolean accept(File f)
    107                {
    108                   return f.getName().toLowerCase().endsWith(".gif") || f.isDirectory();
    109                }
    110 
    111                public String getDescription()
    112                {
    113                   return "GIF Images";
    114                }
    115             });
    116 
    117          // show file chooser dialog
    118          int r = chooser.showOpenDialog(ImageViewerFrame.this);
    119 
    120          // if image file accepted, set it as icon of the label
    121          if (r == JFileChooser.APPROVE_OPTION)
    122          {
    123             String name = chooser.getSelectedFile().getPath();
    124             logger.log(Level.FINE, "Reading file {0}", name);
    125             label.setIcon(new ImageIcon(name));
    126          }
    127          else logger.fine("File open dialog canceled.");
    128          logger.exiting("ImageViewerFrame.FileOpenListener", "actionPerformed");
    129       }
    130    }
    131 }
    132 
    133 /**
    134  * A handler for displaying log records in a window.
    135  */
    136 class WindowHandler extends StreamHandler
    137 {
    138    private JFrame frame;
    139 
    140    public WindowHandler()
    141    {
    142       frame = new JFrame();
    143       final JTextArea output = new JTextArea();
    144       output.setEditable(false);
    145       frame.setSize(200, 200);
    146       frame.add(new JScrollPane(output));
    147       frame.setFocusableWindowState(false);
    148       frame.setVisible(true);
    149       setOutputStream(new OutputStream()
    150          {
    151             public void write(int b)
    152             {
    153             } // not called
    154 
    155             public void write(byte[] b, int off, int len)
    156             {
    157                output.append(new String(b, off, len));
    158             }
    159          });
    160    }
    161 
    162    public void publish(LogRecord record)
    163    {
    164       if (!frame.isVisible()) return;
    165       super.publish(record);
    166       flush();
    167    }
    168 }

     

    实验程序3:

    l 用JDK命令调试运行教材298-300页程序7-2,结合程序运行结果理解程序;

    按课件66-77内容练习并掌握Elipse的常用调试技术。

    package logging;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.logging.*;
    import javax.swing.*;
    
    /**
     * A modification of the image viewer program that logs various events.
     * @version 1.03 2015-08-20
     * @author Cay Horstmann
     */
    public class LoggingImageViewer
    {
       public static void main(String[] args)
       {
          if (System.getProperty("java.util.logging.config.class") == null
                && System.getProperty("java.util.logging.config.file") == null)
          {
             try
             {
                Logger.getLogger("com.horstmann.corejava").setLevel(Level.ALL);
                final int LOG_ROTATION_COUNT = 10;
                Handler handler = new FileHandler("%h/LoggingImageViewer.log", 0, LOG_ROTATION_COUNT);
                Logger.getLogger("com.horstmann.corejava").addHandler(handler);
             }
             catch (IOException e)
             {
                Logger.getLogger("com.horstmann.corejava").log(Level.SEVERE,
                      "Can't create log file handler", e);
             }
          }
    
          EventQueue.invokeLater(() ->
                {
                   Handler windowHandler = new WindowHandler();
                   windowHandler.setLevel(Level.ALL);
                   Logger.getLogger("com.horstmann.corejava").addHandler(windowHandler);
    
                   JFrame frame = new ImageViewerFrame();
                   frame.setTitle("LoggingImageViewer");
                   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
                   Logger.getLogger("com.horstmann.corejava").fine("Showing frame");
                   frame.setVisible(true);
                });
       }
    }
    
    /**
     * The frame that shows the image.
     */
    class ImageViewerFrame extends JFrame
    {
       private static final int DEFAULT_WIDTH = 300;
       private static final int DEFAULT_HEIGHT = 400;   
    
       private JLabel label;
       private static Logger logger = Logger.getLogger("com.horstmann.corejava");
    
       public ImageViewerFrame()
       {
          logger.entering("ImageViewerFrame", "<init>");      
          setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    
          // set up menu bar
          JMenuBar menuBar = new JMenuBar();
          setJMenuBar(menuBar);
    
          JMenu menu = new JMenu("File");
          menuBar.add(menu);
    
          JMenuItem openItem = new JMenuItem("Open");
          menu.add(openItem);
          openItem.addActionListener(new FileOpenListener());
    
          JMenuItem exitItem = new JMenuItem("Exit");
          menu.add(exitItem);
          exitItem.addActionListener(new ActionListener()
             {
                public void actionPerformed(ActionEvent event)
                {
                   logger.fine("Exiting.");
                   System.exit(0);
                }
             });
    
          // use a label to display the images
          label = new JLabel();
          add(label);
          logger.exiting("ImageViewerFrame", "<init>");
       }
    
       private class FileOpenListener implements ActionListener
       {
          public void actionPerformed(ActionEvent event)
          {
             logger.entering("ImageViewerFrame.FileOpenListener", "actionPerformed", event);
    
             // set up file chooser
             JFileChooser chooser = new JFileChooser();
             chooser.setCurrentDirectory(new File("."));
    
             // accept all files ending with .gif
             chooser.setFileFilter(new javax.swing.filechooser.FileFilter()
                {
                   public boolean accept(File f)
                   {
                      return f.getName().toLowerCase().endsWith(".gif") || f.isDirectory();
                   }
    
                   public String getDescription()
                   {
                      return "GIF Images";
                   }
                });
    
             // show file chooser dialog
             int r = chooser.showOpenDialog(ImageViewerFrame.this);
    
             // if image file accepted, set it as icon of the label
             if (r == JFileChooser.APPROVE_OPTION)
             {
                String name = chooser.getSelectedFile().getPath();
                logger.log(Level.FINE, "Reading file {0}", name);
                label.setIcon(new ImageIcon(name));
             }
             else logger.fine("File open dialog canceled.");
             logger.exiting("ImageViewerFrame.FileOpenListener", "actionPerformed");
          }
       }
    }
    
    /**
     * A handler for displaying log records in a window.
     */
    class WindowHandler extends StreamHandler
    {
       private JFrame frame;
    
       public WindowHandler()
       {
          frame = new JFrame();
          final JTextArea output = new JTextArea();
          output.setEditable(false);
          frame.setSize(200, 200);
          frame.add(new JScrollPane(output));
          frame.setFocusableWindowState(false);
          frame.setVisible(true);
          setOutputStream(new OutputStream()
             {
                public void write(int b)
                {
                } // not called
    
                public void write(byte[] b, int off, int len)
                {
                   output.append(new String(b, off, len));
                }
             });
       }
    
       public void publish(LogRecord record)
       {
          if (!frame.isVisible()) return;
          super.publish(record);
          flush();
       }
    }
    LoggingImageViewer

    第三部分:总结

    这周学习java异常处理技术,了解断言的用法,了解日志的用途; 以及学习程序基础调试技巧。对程序编译以及运行时可能出现的异常问题有了初步认识,以及对出现的异常相对相应需要的处理技术。

  • 相关阅读:
    python3--函数(函数,全局变量和局部变量,递归函数)
    Acunetix Web Vulnarability Scanner V10.5 详细中文手册
    Splunk学习与实践
    Visual studio code离线安装插件
    calling c++ from golang with swig--windows dll(一)
    Golang版protobuf编译
    大型网站架构系列:负载均衡详解(3)
    大型网站架构系列:负载均衡详解(2)
    大型网站架构系列:负载均衡详解(1)
    大型网站架构系列:分布式消息队列(二)
  • 原文地址:https://www.cnblogs.com/yqj-yf-111/p/9864490.html
Copyright © 2011-2022 走看看