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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 程有愿 中级黑马   /  2012-7-30 17:54  /  2361 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

/*需求,拷贝一个图像文件*/
import java.io.*;
public class CopPic {
    public static void main(String[] args) throws IOException
    {
  BufferedReader br=new BufferedReader(new InputStreamReader(new  FileInputStream("C:\\Users\\Administrator\\Desktop\\pc201206210003_2.GIF")));
  BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\小外甥1.GIF")));
  String line=null;
  while((line=br.readLine())!=null)
  {
   bw.write(line);
   bw.flush();
  
  }
  
  bw.close();
  br.close();
}
}
为什么报错说找不到指定的路径?

3 个回复

倒序浏览
报错说找不到指定的路径,可能就是你的路径写错了。你检查下试试看。
  1. import java.io.*;

  2. public class Test {
  3.        
  4.         public static void copy()
  5.         {
  6.                 FileInputStream fis = null;
  7.                 FileOutputStream fos = null;
  8.                 BufferedInputStream bufis = null;
  9.                 BufferedOutputStream bufos = null;
  10.                 try
  11.                 {
  12.                         fis = new FileInputStream("C:\\Users\\Administrator\\Desktop\\pc201206210003_2.GIF");
  13.                         fos = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\小外甥1.GIF");
  14.                         bufis = new BufferedInputStream(fis);
  15.                         bufos = new BufferedOutputStream(fos);
  16.                         int line = 0;
  17.                         while((line = bufis.read()) != -1)
  18.                         {
  19.                                 bufos.write(line);
  20.                         }
  21.                 }
  22.                 catch(IOException e)
  23.                 {
  24.                         throw new RuntimeException("读取失败!");
  25.                 }
  26.                 finally
  27.                 {
  28.                         if(bufis != null)
  29.                         try
  30.                         {
  31.                                
  32.                                         bufis.close();
  33.                         }
  34.                         catch(IOException e)
  35.                         {
  36.                                 throw new RuntimeException("读取关闭失败!");
  37.                         }
  38.                         if(bufos != null)
  39.                         try
  40.                         {
  41.                                
  42.                                         bufos.close();
  43.                         }
  44.                         catch(IOException e)
  45.                         {
  46.                                 throw new RuntimeException("写入关闭失败!");
  47.                         }
  48.                 }
  49.         }
  50.        
  51.         public static void main(String[] args)
  52.         {
  53.                 long start = System.currentTimeMillis();
  54.                 copy();
  55.                 long end = System.currentTimeMillis();
  56.                 System.out.println((end - start) + "毫秒");
  57.         }

  58. }
复制代码
回复 使用道具 举报
程序没问题,一定是路径写错了,换个正确的路径就行了”C:\Users\Administrator\Desktop\“  把这个路径复制到地址栏回车,也会提示找不到。很可能你C盘的“Users”或“用户”文件下没有“Adminsitrator”用户名,我开机用户是“sony”所以能找到"C:\Users\sony\Desktop\",我安装Win7时就没创建Administrator用户名。
回复 使用道具 举报
这个不是文本,好像不能用字符串流吧,路径没错,应该用字节流处理吧!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马