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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黄连兵 中级黑马   /  2012-6-29 15:50  /  1704 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public class CopyFile {
        /**
         * @throws FileNotFoundException
         * @拷贝文件
         */
  public static void copyFile(File s,File d) throws IOException{
            FileInputStream fis=new FileInputStream(s);
            FileOutputStream fos=new FileOutputStream(d);
           
          byte[]buf=new byte[1024];
          int len=0;
          try {
                while((len=fis.read(buf))!=-1){
                         fos.write(buf,0,len) ;
                  }
        } catch (IOException e) {
                throw new RuntimeException("文件复制失败");
        }
          finally{
                  try{
                          if (fis!=null)
                                  fis.close();
                  }
                  catch(IOException e){
                          throw new RuntimeException("文件读取未正常关闭");
                  }
          }
  }
  
      public static void main(String[] args) throws IOException{
                // TODO Auto-generated method stub
          File s=new File("F:\\1.java");
          File dir=new File("g:\\1");
          if (!dir.exists()){
                  dir.mkdir();
          }
         // File d=new File(dir,"FileCopy.jad");
          File d=new File(dir,"d:_java_test_src_Hello.jad");
          copyFile(s, d);
          System.out.println("复制成功!");
        }  
}
大家一起分析下:为什么文件名中包含了“:”,程序也正常运行了,拷贝出一个0字节的文件,名字只有“d”!
把“:”去掉就正常了!~

点评

jad?j2me?这个少见啊  发表于 2012-6-29 17:38

4 个回复

倒序浏览
File d=new File(dir,"d:_java_test_src_Hello.jad");
路径等同dir+d:_java_test_src_Hello.jad
系统认定是2个盘符,所以非法路径。
去掉:,就是g:\\1\\d_java_test_src_Hello.jad就没问题了
回复 使用道具 举报
  1. package cn.itcast.heima;

  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;

  6. public class CopyFile {
  7.         public static void copyFile(File s,File d) throws IOException{
  8.                
  9.                 FileInputStream fis=new FileInputStream(s);
  10.         FileOutputStream fos=new FileOutputStream(d);
  11.             
  12.           byte[]buf=new byte[1024];
  13.           int len=0;
  14.           try {
  15.                 while((len=fis.read(buf))!=-1){
  16.                          fos.write(buf,0,len) ;
  17.                   }
  18.         } catch (IOException e) {
  19.                 throw new RuntimeException("文件复制失败");
  20.         }
  21.           finally{
  22.                   try{
  23.                           if (fis!=null)
  24.                                   fis.close();
  25.                   }
  26.                   catch(IOException e){
  27.                           throw new RuntimeException("文件读取未正常关闭");
  28.                   }
  29.           }
  30.   }
  31.   
  32.       public static void main(String[] args) throws IOException{
  33.                 // TODO Auto-generated method stub
  34.           File s=new File("F:\\1.java");
  35.           File dir=new File("g:\\1");
  36.           if (!dir.exists()){
  37.                   dir.mkdir();
  38.           }
  39.          // File d=new File(dir,"FileCopy.jad");
  40.           File d=new File(dir,"d:_java_test_src_Hello.jad");  //这里你写错了。不仔细检查的问题啊。
  41.           copyFile(s, d);
  42.           System.out.println("复制成功!");
  43.         }  
  44. }
复制代码
回复 使用道具 举报
陆强强 发表于 2012-6-29 16:24
File d=new File(dir,"d:_java_test_src_Hello.jad");
路径等同dir+d:_java_test_src_Hello.jad
系统认定是 ...

既然非法了的话,应该会抛出异常才对啊,程序还是执行了,而且还拷贝出了个文件呢~......
回复 使用道具 举报
我把File dir=new File("g:\\1");注释了,
File d=new File("d:_java_test_src_Hello.jad");
复制还是成功了。就是说不管:后面有没有\,系统默认:就是盘符和文件的分隔符。
所以g:\1\d:系统把D当成文件处理,遇到第二个:就不在往下写入了。直接读打印语句结束。


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