您现在的位置: 优易学 >> 学历考试 >> 自学考试 >> 复习资料 >> 专业课复习 >> 正文
Java语言程序设计(一)课后习题第八章(附答案)
来源:优易学 2011-11-25 11:21:08   【优易学:中国教育考试第一门户】   资料下载   学历书店


  12.
  class Excp1 extends Exception
  {}
  class Excp2 extends Excp1
  {}
  public class ExceptionExam7
  {
  public static void main(String [] args) throws Exception
  {
  try
  {
  throw new Excp2();
  }
  catch(Excp2 e)
  {
  System.out.println("catch1");
  throw new Excp1();
  }
  catch(Excp1 e)
  {
  System.out.println("catch2");
  throw new Exception();
  }
  catch(Exception e)
  {
  System.out.println("catch3");
  }
  finally
  {
  System.out.println("finally");
  }
  }
  }
  说明:自定义异常类,关键是选择继承的超类——必须是Exception或者其子类。用异常代表错误,而不要再使用方法返回值。
  13.答:所谓流是指同一台计算机或网络中不同计算机之间有序运动着的数据序列,Java把这些不同来源和目标的数据都统一抽象为数据流。数据流可分为输入流和输出流,输入流代表从其他设备流入计算机的数据序列,输出流代表从计算机流向外部设备的数据序列。
  流式输入输出的特点是数据的获取和发送沿数据序列的顺序进行,即每一个数据都必须等待排在它前面的数据,等前面的数据读入或送出之后才能被读写。所以流和队列一样,只能以“先进先出”的方式对其中的数据进行读写,而不能随意选择读写的位置。
  14.答:两者都作为Object类的直接子类,基本输入流(无效Stream)和基本输出流(OutputStream)是处理以8位字节为基本单位的字节流类;Reader和Writer类是专门处理16位字符流的类。
  15.答:构造函数有:
  (1)public File(String pathname):创建一个对应于参数pathname的File类对象。参数pathname是包含目录和文件名的字符串。如果没有文件名,则代表目录。
  (2)public File(String parent , String child):该构造函数将pathname分成两部分parent和child,参数parent表示目录或文件所在路径,参数child表示目录或文件名称。
  (3)public File(File parent, String child):该构造函数与上面一个不同在于将parent的参数类型由String变为File,代表parent是一个已经创建的File类文件对象(指向目录)。
  常用方法有:
  (1)public boolean canWrite( ):返回文件是否可写。
  (2)public boolean canRead( ):返回文件是否可读。
  (3)public boolean createNewFile( ):当文件不存在时创建文件。
  16.
  import java.io.*;
  class CopyFile{
  public static void main(String[] args) {
  String file1,file2 ;
  int ch = 0 ;
  try {
  file1=args[0];
  file2=args[1];
  File无效Stream fis = new File无效Stream(file1);
  FileOutputStream fos=new FileOutputStream(file2);
  while ((ch=fis.read())!=-1)
  fos.write(ch);
  fis.close();
  fos.close();
  }
  catch(FileNotFoundException e){
  System.out.println("源文件:未找到!");
  }catch(ArrayIndexOutOfBoundsException e){
  System.out.println("缺少运行参数!");
  System.exit(-1);
  }
  catch (IOException e){
  System.out.println(e.toString());
  }
  }
  }
  17.答:
  import java.io.*;
  public class NewFile{
  public static void main(String args[]){
  File f=new File("test");
  if(f.exists()&&f.isDirectory())
  System.err.println("目录:"+f.toString()+"已经存在!");
  else{
  if(f.mkdir()){
  System.out.println("目录"+f.getAbsolutePath()+"创建结束!");
  File f2=new File(f,"my.txt");
  try{
  f2.createNewFile();
  f2.setReadOnly();
  }catch(IOException e){
  System.out.println(e.toString());
  }
  System.out.println("文件:"+f2.getAbsoluteFile()+"创建结束!");
  }
  else
  System.out.println("目录"+f.getAbsoluteFile()+"创建失败!");
  }
  }
  }
  18.答:要实现对文件的随机读取,也就是在文件的任何位置执行读写数据的操作,需要一个指针来指定读写的位置。在创建 RandomAccessFile类对象的同时,系统自动创建了一个指向这个文件开头的指针,当执行读写操作后,指针自动指向被读写数据之后的第一个字节 处。指针初始值是0,每读/写一个字节,指针自动增加1。RandomAccessFile类提供了一些控制指针移动的方法。
  public long getFilePointer();获取当前指针指向文件的位置。
  pulbic void seek(long pos);将指针移动到参数pos指定的位置。
  public int skipBytes(int n);指针从当前位置向后移动n个字节位置,并返回指针实际移动的字节数。
  19.答:
  import java.io.*;
  public class Count{
  public static void main(String[] args)
  {
  int x=0,y=0,z=0;
  int ch;
  try{
  while((ch=System.in.read())!='\r'){
  if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z')
  x++;
  else if(ch>='0'&&ch<='9')
  y++;
  else
  z++;
  }
  }catch(IOException e){
  System.out.println(e.toString());
  }
  System.out.println("英文字母:"+x);
  System.out.println("数字字符:"+y);
  System.out.println("其它字符:"+z);
  }
  }
  20.答:
  import java.io.*;
  public class InFile{
  public static void main(String[] args)
  {
  int ch;
  try{
  FileOutputStream out=new FileOutputStream("a.txt");
  while((ch=System.in.read())!='\r'){
  System.out.write(ch);
  out.write(ch);
  }
  out.close();
  System.out.write('\n');
  }catch(IOException e){
  System.out.println(e.toString());
  }
  System.out.println("输出至文件完毕!");
  }
  }
  21.答:
  import java.io.*;
  public class Sort{
  public static void main(String args[])
  {
  int a[]=new int[10];
  byte b[]=new byte[10];
  int t;
  String str;
  System.out.println("请输入10个整数:");
  try{
  for(int i=0;i<10;i++)
  {System.out.print("No. "+(i+1)+": ");
  System.in.read(b);
  str=new String(b);
  str=str.trim();
  a[i]=Integer.parseInt(str);
  }
  }catch(IOException e){
  System.out.println(e.toString());
  }
  catch(NumberFormatException e){
  System.out.println(e.toString());
  }
  for(int i=0;i<9;i++)
  for(int j=i+1;j<10;j++)
  {
  if(a[i]>a[j]){
  t=a[i];
  a[i]=a[j];
  a[j]=t;
  }
  }
  for(int i=0;i<10;i++)
  System.out.println(a[i]+"\t");
  }
  }

上一页  [1] [2] 

责任编辑:小草

文章搜索:
 相关文章
热点资讯
热门课程培训