站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题Java初学者备忘录

Java初学者备忘录

摘要: 本文以实例讲解了Java的异常处理、输入输出、文件操作、流
<iframe src="http://training.yesky.com/ad/iframe/develop_java.htm" frameBorder=0 width=578 scrolling=no height=62></iframe>

  一.异常

  Java对异常的处理同Delphi一样,不是刻意的去避免它的发生,而是等它发生后去补救.

  Delphi的异常处理简单来说就是一下语句

<table width="100%" bgColor=#ffffff> <tr> <td>Try
Except//异常发生后就转入此处执行
Finally//不管异常发不发生,都转入此处运行
End </td></tr></table>
  与此相类似,Java的异常处理的基本形式如下

<table width="100%" bgColor=#ffffff> <tr> <td>try{
}catch(ExceptionType1 e){
file&://对/异常情况1的处理
}catch(ExceptionType2 e){
file&://对/异常情况2的处理
throw(e)//抛出异常,和Delphi中的raise是一回事
} </td></tr></table>
  要补充的是,对大多数的异常,假如你要在正常运行的程序中而不是捕捉异常的程序中明确的抛出,Java的编译器需要你事先对你要抛出的异常作声明,否则不允许编译通过.这个任务是由throws来完成的.

<iframe align=right marginWidth=0 marginHeight=0 src="http://images.chinabyte.com/adjs/iframe-pip/y-software-pip.html" frameBorder=0 width=360 scrolling=no height=300></iframe>  二.Java的输入输出流

  2.1 输出

<table width="100%" bgColor=#ffffff> <tr> <td>System.out.print  file&://这/里out是一个静态方法哦
System.out.println
System.err.print  file&://err/和out一样也是标准输出,至于有什么不同,我目前还不清楚
System.err.println </td></tr></table>
  2.2 输入

<table width="100%" bgColor=#ffffff> <tr> <td>System.in.read() </td></tr></table>
  2.3 文件的操作

  只需要几个带注释的例子就可以了。

  第一个是一个显示文件基本信息的程序

<table width="100%" bgColor=#ffffff> <tr> <td>import java.io.*;//调入和io相关的类
class fileinfo{
file&://注/意,main函数一定是静态方法

 public static void main(String args[])throws IOException{
  File fileToCheck;//使用文件对象创建实例
  if (args.length>0){
   for (int i=0;i    fileToCheck=new File(args[i]);//为文件对象分配空间
    info(fileToCheck);//这里引用的info一定要是静态方法成员
   }
  }
  else{
   System.out.println("no file given");
  }
 }

 public static void info(File f)throws IOException{
  System.out.println("Name:"+f.getName());
  System.out.println("Path:"+f.getPath());
  if (f.exists()){
   System.out.println("File exists.");
   System.out.print((f.canRead()?" and is Readable":""));//判断函数,如果满足条件,输出前者,否则输出后者
   System.out.print((f.canWrite()?"and is Writable":""));
   System.out.print(".");
   System.out.println("File is"+f.length()+"bytes.");
  }
  else{
   System.out.println("File does not exist.");
  }
 }
}
</td></tr></table>
  第二个例子是一个存储电话信息的小程序,用户输入姓名和电话号码,程序将其存入phone.numbers文件中,通过FileOutputStream来实现

<table width="100%" bgColor=#ffffff> <tr> <td>import java.io.*;

class phones{
 static FileOutputStream fos;
 public static final int lineLength=81;
 public static void main(String args[])throws IOException{
  byte[] phone=new byte[lineLength];
  byte[] name=new byte[lineLength];
  int i;
  fos=new FileOutputStream("phone.numbers");
  while(true){
   System.err.println("Enter a name(enter 'done' to quit)");
   readLine(name);
   if ("done".equalsIgnoreCase(new String(name,0,0,4))){
    break;
   }
   System.err.println("Enter the phone number");
   readLine(phone);
   for (i=0;phone[i]!=0;i++){
    fos.write(phone[i]);
   }
   fos.write(',');
   for (i=0;name[i]!=0;i++){
    fos.write(name[i]);
   }
   fos.write('\n');
  }
  fos.close();
 }

 private static void readLine(byte line[])throws IOException{
  int i=0,b=0;
  while((i<(lineLength-1))&&((b=System.in.read())!='\n')){
   line[i++]=(byte)b;
  }
  line[i]=(byte)(0);
 }
} </td></tr></table>




↑返回目录
前一篇: 使用Digester解析XML文档示例
后一篇: DB2 J2EE开发成本收益分析(二)