A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 贾存双 中级黑马   /  2012-7-17 20:53  /  2159 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 贾存双 于 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 个错误

3 个回复

倒序浏览
作用域问题 在try catch里面定义 只能作用于try catch内
回复 使用道具 举报
  1. try{
  2.     InputStream input = new FileInputStream(f1) ;   为什么在try里面这实例化就是错误的?为什么要在try 外面声明才可以???
  3.     OutputStream out = new FileOutputStream(f2) ;
  4.    }
复制代码
在里面声明的话,这两个流就只作用在try的这对大括号内,作用域的问题! 你在后面再去用当然就出错了!
回复 使用道具 举报
本帖最后由 包寅星 于 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() ;
   }
  }
}
}

评分

参与人数 1技术分 +1 收起 理由
刘笑 + 1 赞一个!

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马