黑马程序员技术交流社区

标题: 有关于将d盘下某个文件中的全部内容复制到e盘 [打印本页]

作者: 烟默year    时间: 2014-10-22 19:21
标题: 有关于将d盘下某个文件中的全部内容复制到e盘
将E盘下某个文件中的全部内容(文本、MP3、文件夹等)复制到D盘下某个文件夹。
恳求代码!

作者: 游客也爱学    时间: 2014-10-25 21:59
/**
     * 复制单个文件
     * @param oldPath String 原文件路径 如:c:/fqf.txt
     * @param newPath String 复制后路径 如:f:/fqf.txt
     * @return boolean
     */
   public void copyFile(String oldPath, String newPath) {
       try {
           int bytesum = 0;
           int byteread = 0;
           File oldfile = new File(oldPath);
           if (oldfile.exists()) { //文件存在时
               InputStream inStream = new FileInputStream(oldPath); //读入原文件
               FileOutputStream fs = new FileOutputStream(newPath);
               byte[] buffer = new byte[1444];
               int length;
               while ( (byteread = inStream.read(buffer)) != -1) {
                   bytesum += byteread; //字节数 文件大小
                   System.out.println(bytesum);
                   fs.write(buffer, 0, byteread);
               }
               inStream.close();
           }
       }
       catch (Exception e) {
           System.out.println("复制单个文件操作出错");
           e.printStackTrace();

       }

   }

作者: 烟默year    时间: 2014-10-26 17:16
游客也爱学 发表于 2014-10-25 21:59
/**
     * 复制单个文件
     * @param oldPath String 原文件路径 如:c:/fqf.txt

很详细!谢谢了




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2