FileInputStream FileNotFoundException知道问题在哪里,不知道怎么产生的

CaptainGan 2011-11-14 05:37:22
就一行代码:

// 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 不能操作文件夹,只能操作文件。

大家说说我错在什么地方?多谢大家指教。
...全文
973 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
CaptainGan 2011-11-14
  • 打赏
  • 举报
回复
花了我晚上2个小时,终于整出来了,就是不知道数据量很大的时候,效率怎么样,需要实践的检验。

分享到了CSDN 博客上了,欢迎大家多多指教。

http://blog.csdn.net/captaingan/article/details/6970332
龙腾冰 2011-11-14
  • 打赏
  • 举报
回复
public class FileNotFoundExceptionextends IOException当试图打开指定路径名表示的文件失败时,抛出此异常。

在不存在具有指定路径名的文件时,此异常将由 FileInputStream、FileOutputStream 和 RandomAccessFile 构造方法抛出。如果该文件存在,但是由于某些原因不可访问,比如试图打开一个只读文件进行写入,则此时这些构造方法仍然会抛出该异常。
xiongyu2006 2011-11-14
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 captaingan 的回复:]

引用 4 楼 xiongyu2006 的回复:
判断服务器上有没有文件夹,如果没有就创建,然后再写文件。


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

这样感觉有点麻烦。
[/Quote]
没有文件夹肯定不能用流输出文件的,其实也不麻烦遍历一下,然后判断exists()没有就mkidr()或者mkdirs()还然后再拼凑一个新的File,还是是挺方便的
chabale 2011-11-14
  • 打赏
  • 举报
回复
不知道你是不是对文件进行复制,
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();
chabale 2011-11-14
  • 打赏
  • 举报
回复
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();
CaptainGan 2011-11-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xiongyu2006 的回复:]
判断服务器上有没有文件夹,如果没有就创建,然后再写文件。
[/Quote]

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

这样感觉有点麻烦。
xiongyu2006 2011-11-14
  • 打赏
  • 举报
回复
判断服务器上有没有文件夹,如果没有就创建,然后再写文件。
CaptainGan 2011-11-14
  • 打赏
  • 举报
回复
在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 处理上传文件夹的情况?
huntor 2011-11-14
  • 打赏
  • 举报
回复
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.
huntor 2011-11-14
  • 打赏
  • 举报
回复
上面的异常貌似是FileInputStream 不能操作文件夹,只能操作文件。

就是这个原因。
package com.test; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class B { public static void main(String[] args) { File file = new File("c:\\a.jpg"); FileInputStream is = null; try { is = new FileInputStream(file); } catch (FileNotFoundException e2) { e2.printStackTrace(); // return rect; } BufferedImage sourceImg = null; try { sourceImg = javax.imageio.ImageIO.read(is); } catch (IOException e1) { e1.printStackTrace(); // return rect; } System.out.println("width = " + sourceImg.getWidth() + "height = " + sourceImg.getHeight()); } } package com.test; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class B { public static void main(String[] args) { File file = new File("c:\\a.jpg"); FileInputStream is = null; try { is = new FileInputStream(file); } catch (FileNotFoundException e2) { e2.printStackTrace(); // return rect; } BufferedImage sourceImg = null; try { sourceImg = javax.imageio.ImageIO.read(is); } catch (IOException e1) { e1.printStackTrace(); // return rect; } System.out.println("width = " + sourceImg.getWidth() + "height = " + sourceImg.getHeight()); } } package com.test; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class B { public static void main(String[] args) { File file = new File("c:\\a.jpg"); FileInputStream is = null; try { is = new FileInputStream(file); } catch (FileNotFoundException e2) { e2.printStackTrace(); // return rect; } BufferedImage sourceImg = null; try { sourceImg = javax.imageio.ImageIO.read(is); } catch (IOException e1) { e1.printStackTrace(); // return rect; } System.out.println("width = " + sourceImg.getWidth() + "height = " + sourceImg.getHeight()); } } package com.test; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class B { public static void main(String[] args) { File file = new File("c:\\a.jpg"); FileInputStream is = null; try { is = new FileInputStream(file); } catch (FileNotFoundException e2) { e2.printStackTrace(); // return rect; } BufferedImage sourceImg = null; try { sourceImg = javax.imageio.ImageIO.read(is); } catch (IOException e1) { e1.printStackTrace(); // return rect; } System.out.println("width = " + sourceImg.getWidth() + "height = " + sourceImg.getHeight()); } }

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧