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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ....... 中级黑马   /  2014-2-21 22:19  /  1168 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

怎么提示这个呀
java.io.FileNotFoundException: e:\Open_Sourse (拒绝访问。)        at java.io.FileInputStream.open(Native Method)        at java.io.FileInputStream.<init>(FileInputStream.java:106)        at java.io.FileReader.<init>(FileReader.java:55)        at CopyFilesDemo.copyFiles(CopyFilesDemo.java:20)        at CopyFilesDemo.main(CopyFilesDemo.java:9)Exception in thread "main" java.lang.RuntimeException: 文件复制失败!        at CopyFilesDemo.copyFiles(CopyFilesDemo.java:28)        at CopyFilesDemo.main(CopyFilesDemo.java:9)








import java.io.*;

class CopyFilesDemo
{
        public static void main(String[] args)
        {
                String sourse = "e:\\Open_Sourse";
                String target = "e:\\copy";
                copyFiles(sourse,target);
        }

        public static void copyFiles(String sourse,String target)
        {
          File sour = new File(sourse);
                  File targ = new File(target);
                  Writer fw = null;
                  BufferedReader fr = null;
                  try
                  {
                          fr = new BufferedReader(new FileReader(sour));
                          fw = new BufferedWriter(new FileWriter(targ));
              copyToFile(sour,targ,fr,fw);
                       
                  }
                  catch (IOException e)
                  {
                          e.printStackTrace();
                          throw new RuntimeException("文件复制失败!");
                  }finally
                  {
                      try
                      {
                                if(fr != null)
                                        fr.close();
                      }
                      catch (IOException efr)
                      {
                                  efr.printStackTrace();
                                  throw new RuntimeException("读取流关闭失败!");
                      }
                          try
                          {
                                if(fw != null)
                                        fw.close();
                          }
                          catch (IOException efw)
                          {
                                  efw.printStackTrace();
                                  throw new RuntimeException("写入流关闭失败!");
                          }
                  }
        }

        public static void copyToFile(File sour,File targ,BufferedReader fr,Writer fw) throws IOException
        {
                File[] files = sour.listFiles();

                for(File file : files)
                {
                   if(file.isDirectory())
                           copyToFile(sour,targ,fr,fw);
                   else
                   {
                       String line = null;
                           while((line = fr.readLine()) != null)
                           {
                                   fw.write(line);
                                   fw.flush();
                           }
                   }
                }
        }
}


评分

参与人数 1技术分 +1 收起 理由
何伟超 + 1

查看全部评分

6 个回复

倒序浏览
你太................
回复 使用道具 举报
  String sourse = "e:\\Open_Sourse";
  String target = "e:\\copy";

fr = new BufferedReader(new FileReader(sour));
fw = new BufferedWriter(new FileWriter(targ));
你这new的FileReader和FileWriter传的是目录,所以错了
应该在判断是文件的时候把文件传进去。

评分

参与人数 1技术分 +1 收起 理由
何伟超 + 1

查看全部评分

回复 使用道具 举报
文件夹是不能建立流对象的
这样就可以了
  1. package Test;

  2. import java.io.*;

  3. public class Test2
  4. {
  5.         public static void main(String[] args) throws IOException
  6.         {
  7.                 String sourse = "F:\\mp3";
  8.                 String target = "F:\\copy";
  9.                 copyToFile(new File(sourse),new File(target));
  10.         }

  11.         public static void copyFiles(String sourse,String target)
  12.         {
  13.                   File sour = new File(sourse);
  14.                   File targ = new File(target);
  15.                   Writer fw = null;
  16.                   BufferedReader fr = null;
  17.                   try
  18.                   {
  19.                           if(sour.isFile()){
  20.                                   fr = new BufferedReader(new FileReader(sour));
  21.                           fw = new BufferedWriter(new FileWriter(targ));
  22.                           }
  23.                       copyToFile(sour,targ);
  24.                         
  25.                   }
  26.                   catch (IOException e)
  27.                   {
  28.                           e.printStackTrace();
  29.                           throw new RuntimeException("文件复制失败!");
  30.                   }finally
  31.                   {
  32.                       try
  33.                       {
  34.                                 if(fr != null)
  35.                                         fr.close();
  36.                       }
  37.                       catch (IOException efr)
  38.                       {
  39.                                   efr.printStackTrace();
  40.                                   throw new RuntimeException("读取流关闭失败!");
  41.                       }
  42.                           try
  43.                           {
  44.                                 if(fw != null)
  45.                                         fw.close();
  46.                           }
  47.                           catch (IOException efw)
  48.                           {
  49.                                   efw.printStackTrace();
  50.                                   throw new RuntimeException("写入流关闭失败!");
  51.                           }
  52.                   }
  53.         }

  54.         public static void copyToFile(File sour,File targ) throws IOException
  55.         {
  56.                //建立目标文件夹
  57.                
  58.                targ.mkdirs();
  59.                 File[] files = sour.listFiles();
  60.                 for(File file : files)
  61.                 {      
  62.                         if(file.isDirectory()){
  63.                                 File target =
  64.                                                 new File(targ.getAbsoluteFile()+File.separator+file.getName());
  65.                            copyToFile(file,target);
  66.                    }        
  67.                    else
  68.                    {
  69.                            BufferedReader bufr =
  70.                                            new BufferedReader(new FileReader(file));
  71.                            BufferedWriter bufw =
  72.                                            new BufferedWriter(new FileWriter(targ.getAbsolutePath()+File.separator+file.getName()));
  73.                            String line = null;
  74.                            while((line = bufr.readLine()) != null)
  75.                            {
  76.                                    bufw.write(line);
  77.                                    bufw.flush();
  78.                            }
  79.                    }
  80.                 }
  81.         }
  82. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
何伟超 + 1

查看全部评分

回复 使用道具 举报
薆情媬証書 来自手机 中级黑马 2014-2-22 04:44:00
报纸
楼主前面定义的两个字符串应该是代表
回复 使用道具 举报
薆情媬証書 来自手机 中级黑马 2014-2-22 04:50:53
地板
楼主前面定义的两个字符串应该是代表两个文件的吧,可是你有没有发现你没有写文件的后缀啊,这样就成了文件夹了,但是你硬盘中又没有这一文件夹,而文件夹复制到文件夹是不可以的,所以楼主应该在定义文件路径的时候将文件的后缀名加上

评分

参与人数 1技术分 +1 收起 理由
何伟超 + 1

查看全部评分

回复 使用道具 举报
如果指定文件不存在,或者它是一个目录,而不是一个常规文件,抑或因为其他某些原因而无法打开进行读取,则抛出 FileNotFoundException。——摘自API帮助文档
从上面的描述可以知道,在四种情况下会抛出该异常:
1、该文件不存在
2、你给的路径是一个目录而不是一个文件
3、文件为非常规文件,访问权限受限
4、因为其他原因,如另一个程序正在使用该文件
**********
根据你的代码,应该是第2个原因导致抛出该异常
在文件夹后面加上一个具体的文件即可解决这个问题

评分

参与人数 1技术分 +1 收起 理由
何伟超 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马