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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王春蕾   /  2014-5-20 10:11  /  5316 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

我定义了一个方法来使用BufferedInputStream和BufferedOutputStream来复制图片,代码如下:
                String fromPath = "D:\\aa.png";
                String toPath = "C:\\a.png";
  1.         private static void copyPicByBuffer(String fromPath, String toPath) {
  2.                 // TODO Auto-generated method stub
  3.                 BufferedInputStream bis = null;
  4.                 BufferedOutputStream bos = null;
  5.                 try {
  6.                         bis = new BufferedInputStream(new FileInputStream("fromPath"));
  7.                         bos = new BufferedOutputStream(new FileOutputStream("toPath"));
  8.                         byte[] buf = new byte[1024];
  9.                         int len = 0;
  10.                         while ((len = bis.read(buf)) != -1) {
  11.                                 bos.write(buf, 0, len);
  12.                                 bos.flush();
  13.                         }
  14.                         System.out.println("Finished copying picture by buffer");
  15.                 } catch (IOException e) {
  16.                         // TODO Auto-generated catch block
  17.                         e.printStackTrace();
  18.                 } finally {
  19.                         if (bis != null) {
  20.                                 try {
  21.                                         bis.close();
  22.                                 } catch (IOException e) {
  23.                                         // TODO Auto-generated catch block
  24.                                         e.printStackTrace();
  25.                                 }
  26.                         }
  27.                         if(bos!=null){
  28.                                 try {
  29.                                         bos.close();
  30.                                 } catch (IOException e) {
  31.                                         // TODO Auto-generated catch block
  32.                                         e.printStackTrace();
  33.                                 }
  34.                         }
  35.                 }
  36.         }
复制代码

为什么运行的时候提示java.io.FileNotFoundException: fromPath (系统找不到指定的文件。)错误啊!
我确定我指定的路径文件都是存在的啊!

评分

参与人数 1技术分 +1 收起 理由
李小然 + 1

查看全部评分

7 个回复

倒序浏览
在new流对象的时候路径不用引号。还有个字节流不用flush。
回复 使用道具 举报
bis = new BufferedInputStream(new FileInputStream("fromPath"));                         bos = new BufferedOutputStream(new FileOutputStream("toPath"));

评分

参与人数 1技术分 +1 收起 理由
李小然 + 1

查看全部评分

回复 使用道具 举报
你应该是传递的参数,如果你加了引号了  系统自动去找fromPath这个问价,当然会出现文件未找到异常了。
回复 使用道具 举报
还有一个问题,FileOutPutStream 里面的参数是一个字符串,那么表示你复制后的文件名为toPath并且在你的c盘下。你的目的是将c盘的东西复制到d盘,因此可以传递一个file类型的参数才行,或者将topath封装成file。
回复 使用道具 举报
bis = new BufferedInputStream(new FileInputStream("fromPath"))
  bos = new BufferedOutputStream(new FileOutputStream("toPath"));
这里传的fromPfath和toPath不可以用“”括起来!!这是你定义的变量,你括起来就成了字符串了
回复 使用道具 举报
new FileInputStream("fromPath")//这里你传进去的是字符串“formPath”,而不是fromPath这个变量了,所以说找不到文件啦
回复 使用道具 举报
还没学到这里,感觉好复杂的样子
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马