黑马程序员技术交流社区

标题: 在try catch 中实例化怎么会出错??? [打印本页]

作者: 贾存双    时间: 2012-7-17 20:53
标题: 在try catch 中实例化怎么会出错???
本帖最后由 贾存双 于 2012-7-18 00:05 编辑

import java.io.* ;
public class Copy{
public static void main(String args[]){
  if(args.length!=2){
   System.out.println("您输入的参数不正确!") ;
   System.out.println("格式为:java Copy 源文件路径 目标文件路径") ;
   System.exit(1) ;
  }
  File f1 = new File(args[0]) ;
  File f2 = new File(args[1]) ;
  
  if(!f1.exists()){
   System.out.println("源文件不存在!") ;
   System.exit(1) ;
  }
  if(!f2.exists()){
   System.out.println("源文件不存在!") ;
   System.exit(1) ;
  }
//InputStream input = null ;      把  input 和 out 在这声明是正确的,这四句注释掉的是写法正确的,try里面没注释掉的写法是错误的。
//OutputStream out = null ;     
  try{
   //input = new FileInputStream(f1) ;
   //out = new FileOutputStream(f2) ;
   InputStream input = new FileInputStream(f1) ;   为什么在try里面这实例化就是错误的?为什么要在try 外面声明才可以???
   OutputStream out = new FileOutputStream(f2) ;
  }catch(Exception e){
   e.printStackTrace() ;
  }
  if(f1!=null && f2!=null){
   int temp = 0 ;
   try{
    while((temp=input.read()) != -1){
     out.write(temp) ;
    }
    System.out.println("拷贝完成 ! ") ;
   }catch(Exception e){
    e.printStackTrace() ;
    System.out.println("拷贝失败! ") ;
   }
   try{
    input.close() ;
    out.close() ;
   }catch(Exception e){
    e.printStackTrace() ;
   }
  }
}
}
错误结果为:
E:\io\Stream\Copy\Copy.java:31: 错误: 找不到符号
    while((temp=input.read()) != -1){
                ^
  符号:   变量 input
  位置: 类 Copy
E:\io\Stream\Copy\Copy.java:32: 错误: 找不到符号
     out.write(temp) ;
     ^
  符号:   变量 out
  位置: 类 Copy
E:\io\Stream\Copy\Copy.java:40: 错误: 找不到符号
    input.close() ;
    ^
  符号:   变量 input
  位置: 类 Copy
E:\io\Stream\Copy\Copy.java:41: 错误: 找不到符号
    out.close() ;
    ^
  符号:   变量 out
  位置: 类 Copy
4 个错误
作者: 马林康    时间: 2012-7-17 21:00
作用域问题 在try catch里面定义 只能作用于try catch内
作者: 黑马张扬    时间: 2012-7-17 21:01
  1. try{
  2.     InputStream input = new FileInputStream(f1) ;   为什么在try里面这实例化就是错误的?为什么要在try 外面声明才可以???
  3.     OutputStream out = new FileOutputStream(f2) ;
  4.    }
复制代码
在里面声明的话,这两个流就只作用在try的这对大括号内,作用域的问题! 你在后面再去用当然就出错了!
作者: 包寅星    时间: 2012-7-17 21:40
本帖最后由 包寅星 于 2012-7-17 21:44 编辑

1.
//InputStream input = null ;      把  input 和 out 在这声明是正确的,这四句注释掉的是写法正确的,try里面没注释掉的写法是错误的。
//OutputStream out = null ;     
注:此处声明的变量为main内的变量。 在整个main方法内可以全局调用。
2.
  try{
   //input = new FileInputStream(f1) ;
   //out = new FileOutputStream(f2) ;
    InputStream input = new FileInputStream(f1) ;   为什么在try里面这实例化就是错误的?为什么要在try 外面声明才可以???
   OutputStream out = new FileOutputStream(f2) ;
  }
注:两种声明方式变量的作用域不同。
InputStream input = new FileInputStream(f1) ;   为什么在try里面这实例化就是错误的?为什么要在try 外面声明才可以???
   OutputStream out = new FileOutputStream(f2) ;
此处声明的InputStream类型的变量input是try{}内部的变量,只能在try{}内才能访问。


正确代码如下


import java.io.* ;
public class Copy{
public static void main(String args[]){
  if(args.length!=2){
   System.out.println("您输入的参数不正确!") ;
   System.out.println("格式为:java Copy 源文件路径 目标文件路径") ;
   System.exit(1) ;
  }
  File f1 = new File(args[0]) ;
  File f2 = new File(args[1]) ;
  
  if(!f1.exists()){
   System.out.println("源文件不存在!") ;
   System.exit(1) ;
  }
  if(!f2.exists()){
   System.out.println("源文件不存在!") ;
   System.exit(1) ;
  }
InputStream input = null ;    //  此处声明的变量在main方法内全局调用。
OutputStream out = null ;     
  try{
   //input = new FileInputStream(f1) ;
   //out = new FileOutputStream(f2) ;
  input = new FileInputStream(f1) ;   //原来声明的变量为try局部变量。
   out = new FileOutputStream(f2) ;
  }catch(Exception e){
   e.printStackTrace() ;
  }
  if(f1!=null && f2!=null){
   int temp = 0 ;
   try{
    while((temp=input.read()) != -1){
     out.write(temp) ;
    }
    System.out.println("拷贝完成 ! ") ;
   }catch(Exception e){
    e.printStackTrace() ;
    System.out.println("拷贝失败! ") ;
   }
   try{
    input.close() ;
    out.close() ;
   }catch(Exception e){
    e.printStackTrace() ;
   }
  }
}
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2