就一行代码:// A:
FileInputStream inputStream = new FileInputStream(new File("D:\\NEW_IMAGE"));//产生异常
// B:
File file = new File("D:\\NEW_IMAGE");
FileInputStream inputStream = new FileInputStream(file);//产生异常上面两种写法都会产生同一个异常。
可以确定的是D:\\NEW_IMAGE这个目录是存在的。
产生的异常为:
java.io.FileNotFoundException: D:\NEW_IMAGE (拒绝访问。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
FileInputStream inputStream = new FileInputStream("D:\\NEW_IMAGE\\a.txt"));//这样些就不会产生异常
上面的异常貌似是FileInputStream 不能操作文件夹,只能操作文件。大家说说我错在什么地方?多谢大家指教。

解决方案 »

  1.   

    上面的异常貌似是FileInputStream 不能操作文件夹,只能操作文件。就是这个原因。
      

  2.   

        FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
      

  3.   

    在FTP上传文件的时候,会有上传文件夹的情况,如果用FileInputStream就肯定产生异常了。下面代码,就只能上传文件,不能上传文件夹。
    public boolean uploadFileToFtp(String fileName, InputStream input,
    String toFtpPath) throws IOException {
    boolean bool = false;
    // 获得Ftp根目录路径中的文件列表
    FTPFile[] ftpFiles = ftpClient.listFiles();
    // 使得能够处理中文编码
    fileName = new String(fileName.getBytes("GBK"), "ISO-8859-1");
    toFtpPath = new String(toFtpPath.getBytes("GBK"), "ISO-8859-1");
    // 转到上传文件的FTP目录中
    ftpClient.changeWorkingDirectory(toFtpPath);
    // 设置处理文件的类型为字节流的形式
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    // 如果缺省该句 传输txt正常 但图片和其他格式的文件传输出现乱码
    ftpClient.storeFile(fileName, input);
    input.close();
    bool = true;
    if (ftpClient.isConnected()) {
    try {
    ftpClient.disconnect();
    } catch (IOException ioe) {
    ioe.printStackTrace();
    }
    }
    return bool;
    }怎样使用Apache FTP 处理上传文件夹的情况?
      

  4.   


    如果文件夹中有很多层级的文件,这样就很麻烦了。只有写个递归方法遍历文件夹中的文件目录,逐个判断是不是文件夹类型,是文件夹类型在FTP上新建个文件夹。如果是文件类型,上传到指定新建的文件夹目录下。这样感觉有点麻烦。
      

  5.   

    FileOutputStream fout = new FileOutputStream("D:/NEW_IMAGE");
    DataOutputStream dout = new DataOutputStream(fout);
    long start = System.currentTimeMillis();
    //读取原文件的输入流
    FileInputStream f = new FileInputStream("d:/heihei.RMVB");
    DataInputStream d = new DataInputStream(f);

    //-创建一个byte类型数组,定义数据包的大小为2048 (2kb)
    byte b[] = new byte[2048];

    int i = d.read(b);//读取文件的内容返回值是 本次读取到的字节的长度
    while(i != -1){
    dout.write(b,0,i);
    dout.flush();
    i = d.read(b);
    }
    f.close();
    d.close();
    fout.close();
    dout.close();
      

  6.   

    不知道你是不是对文件进行复制,
    FileOutputStream fout = new FileOutputStream("目标文件路径");
    DataOutputStream dout = new DataOutputStream(fout);
    long start = System.currentTimeMillis();
    //读取原文件的输入流
    FileInputStream f = new FileInputStream("源文件路径");
    DataInputStream d = new DataInputStream(f);//-创建一个byte类型数组,定义数据包的大小为2048 (2kb)
    byte b[] = new byte[2048];int i = d.read(b);//读取文件的内容返回值是 本次读取到的字节的长度
    while(i != -1){
    dout.write(b,0,i);
    dout.flush();
    i = d.read(b);
    }
    f.close();
    d.close();
    fout.close();
    dout.close(); 
      

  7.   

    没有文件夹肯定不能用流输出文件的,其实也不麻烦遍历一下,然后判断exists()没有就mkidr()或者mkdirs()还然后再拼凑一个新的File,还是是挺方便的
      

  8.   

    public class FileNotFoundExceptionextends IOException当试图打开指定路径名表示的文件失败时,抛出此异常。 在不存在具有指定路径名的文件时,此异常将由 FileInputStream、FileOutputStream 和 RandomAccessFile 构造方法抛出。如果该文件存在,但是由于某些原因不可访问,比如试图打开一个只读文件进行写入,则此时这些构造方法仍然会抛出该异常。 
      

  9.   

    花了我晚上2个小时,终于整出来了,就是不知道数据量很大的时候,效率怎么样,需要实践的检验。分享到了CSDN 博客上了,欢迎大家多多指教。http://blog.csdn.net/captaingan/article/details/6970332