try {
if(!srcFile.exists()) {
srcFile.createNewFile();
}
in = new FileInputStream(src);
} catch (FileNotFoundException e) {
System.out.println("您要拷贝的文件并不存在");
} catch (IOException e) {

}如上代码会提示抛出异常,Eclipse仅能提示是IOException。请问这种创建文件的操作会产生何种异常。
望大家不吝赐教!

解决方案 »

  1.   


    package com.walkman.io;import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;/**
     * 作者: walkman
     * 日期: 2011-7-22
     * 内容: 以文件流的方式复制文件
     */
    public class CopyByStream { public static void main(String[] args) {
    CopyByStream cbs = new CopyByStream();
    String srcFile = "E:\\java_content\\TestForStreamCopy.log";
    String destFile = "E:\\java_content\\TestForStreamCopyDest.txt";
    cbs.copyFile(srcFile, destFile);
    } public void copyFile(String src, String dest){
    FileInputStream in = null;
    FileOutputStream out = null;

    File srcFile = null;

    int c;
    byte buffer[] = new byte[1024];


    try {
    srcFile = new File(src);
    if(!srcFile.exists()) {
    srcFile.createNewFile();
    }

    in = new FileInputStream(src);
    while((c = in.read(buffer)) != -1 ) {
    for(int i = 0; i < c; i++) {
    out.write(buffer[i]);
    }
    }
    } catch (FileNotFoundException e) {
    System.out.println("您要拷贝的文件并不存在");
    } catch (IOException e) {
    System.out.println("读写异常");
    } finally {
    try {
    if (in != null) {
    in.close();
    }
    if(out!= null) {
    out.close();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    }
    }我需要异常信息提示的尽量完善,把可能发生的情况考虑到。
      

  2.   

     NullPointerException - 如果 src 参数为 null
     java.net.URI src  maybe throw IllegalArgumentException 
      

  3.   

    应该是先创建java_content这个目录,再创建文件,还有LZ没有判断destFile存在不存在
      

  4.   

    再加一句catch 捕获所有类型的异常即可。catch (Exception e) {
        System.out.println("捕获所有类型的异常");
    }
            
      

  5.   

    在catch里加一句话:catch (IOException e) {
    e.printStackTrace();
    System.out.println("读写异常");
    } 会打印出问题,看你的代码,感觉你的逻辑很混乱。。