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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 种生祥 于 2012-11-24 11:43 编辑
  1. /**
  2. 把C盘中所有目录里的JPG文件拷贝到当前目录下

  3. */

  4. import java.io.*;
  5. class CCopyToD2
  6. {
  7.         
  8.         public static void main(String[] args) throws IOException
  9.         {
  10.                 File dir = new File("C:");
  11.                
  12.                 copyPic(dir);
  13.         }
  14.         
  15.         public static void copyPic(File dir) throws IOException
  16.         {
  17.                 File[] files = dir.listFiles();
  18.                 for(File f : files)
  19.                 {
  20.                         if(f.isDirectory())
  21.                                 copyPic(f);
  22.                         else
  23.                                 if(f.getName().endsWith(".jpg"))
  24.                                 {
  25.                                        
  26.                                         BufferedInputStream fi = new BufferedInputStream(new FileInputStream(f.getAbsolutePath()));
  27.                                         BufferedOutputStream fo = new BufferedOutputStream(new FileOutputStream(f.getName()));
  28.                                        
  29.                                         int by = 0;
  30.                                         while((by=fi.read())!=-1)
  31.                                                 fo.write(by);

  32.                                         fo.close();
  33.                                         fi.close();
  34.                                 }
  35.                 }

  36.         }
  37. }
复制代码
拷贝时,为什么拷贝不完全,就是没有把C盘所有.jpg文件进行拷贝。
如果要复制到一个绝对路径,又要这么设置?

未命名.jpg (19.6 KB, 下载次数: 33)

未命名.jpg

评分

参与人数 1技术分 +1 收起 理由
古银平 + 1 神马都是浮云

查看全部评分

13 个回复

倒序浏览
    File[] files = dir.listFiles();

这个数组应该是空的

评分

参与人数 1技术分 +1 收起 理由
崔政 + 1 神马都是浮云

查看全部评分

回复 使用道具 举报
public static void copyPic(File dir,File dirTo) throws IOException
        {
                File[] files = dir.listFiles();
这里加个if(files == null)
        return;
判断是否为空,设置绝对路径只要新建一个文件就可以了

评分

参与人数 1技术分 +1 收起 理由
崔政 + 1 赞一个!

查看全部评分

回复 使用道具 举报
楼上的哥们说的是正确的,忽略了要是目录出现空的情况,

那么file[] files 对象将为空,这个操作一个空的文件数组,必然是空指针异常。

完整代码如下:
  1. package com.heima.test;

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

  7. public class CopyTest {

  8.         /**
  9.          * @param args
  10.          */
  11.         public static void main(String[] args) {
  12.                 // TODO Auto-generated method stub
  13.        
  14.                 File dir = new File("E:");
  15.                 copyPic(dir);
  16.         }
  17.        
  18.         public static void copyPic(File dir){
  19.                
  20.                 File[] files = dir.listFiles();
  21.                 if(files!=null){ //文件类型数组对象不为空的时候进行操作
  22.                         for(File f : files){
  23.                                 if(f.isDirectory())
  24.                                         copyPic(f);
  25.                                 else
  26.                                 {               
  27.                                         if(f.getName().endsWith(".jpg")){                       
  28.                                                 copyMethod(f);                                       
  29.                                         }
  30.                                 }
  31.                         }
  32.                 }
  33.         }

  34.         private static void copyMethod(File f) {
  35.                
  36.                 FileInputStream fis = null;
  37.                 FileOutputStream fos = null;
  38.                 try {
  39.                         fis = new FileInputStream(f.getAbsolutePath());
  40.                         fos = new FileOutputStream("C:\\Users\\hp\\Desktop\\jpgList\\"+f.getName());
  41.                         byte[] buf = new byte[1024];
  42.                         int len = 0;
  43.                         while((len=fis.read(buf))!=-1){
  44.                                 fos.write(buf, 0, len);
  45.                         }
  46.                 } catch (FileNotFoundException e) {
  47.                         e.printStackTrace();
  48.                 } catch (IOException e) {
  49.                         e.printStackTrace();
  50.                 }finally{
  51.                         if(fis!=null){
  52.                                 try {
  53.                                         fis.close();
  54.                                 } catch (IOException e) {
  55.                                         e.printStackTrace();
  56.                                 }                               
  57.                         }
  58.                         if(fos!=null){
  59.                                 try {
  60.                                         fos.close();
  61.                                 } catch (IOException e) {
  62.                                         e.printStackTrace();
  63.                                 }                                                       
  64.                         }                       
  65.                 }
  66.         }
  67. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
崔政 + 1 赞一个!

查看全部评分

回复 使用道具 举报
叶桂宏 发表于 2012-11-23 17:05
代码如下看注释,请多多指教:
/*
把C盘中所有目录里的JPG文件拷贝到当前目录下

受教了,3q
回复 使用道具 举报
李志阳 发表于 2012-11-23 18:04
public static void copyPic(File dir,File dirTo) throws IOException
        {
                File[]  ...

原来这这里的问题,谢了{:soso_e183:}
回复 使用道具 举报
杨卫腾 发表于 2012-11-23 19:04
楼上的哥们说的是正确的,忽略了要是目录出现空的情况,

那么file[] files 对象将为空,这个操作一个空的 ...

{:soso_e183:}
回复 使用道具 举报
李志阳 发表于 2012-11-23 18:04
public static void copyPic(File dir,File dirTo) throws IOException
        {
                File[]  ...

dir 是盘符跟是文件夹的时候有什么区别,经测试dir是盘符时,会报异常,是文件夹时不会,这是为什么?
回复 使用道具 举报
filter 中级黑马 2012-11-23 22:25:09
9#
种生祥 发表于 2012-11-23 22:01
dir 是盘符跟是文件夹的时候有什么区别,经测试dir是盘符时,会报异常,是文件夹时不会,这是为什么? ...

盘符跟文件夹都是表示文件路径啊,是不是盘符不存在,还是格式错误什么的,最好贴图上来
回复 使用道具 举报
李志阳 发表于 2012-11-23 22:25
盘符跟文件夹都是表示文件路径啊,是不是盘符不存在,还是格式错误什么的,最好贴图上来 ...

就这样,文件夹跟盘符
更多图片 小图 大图
组图打开中,请稍候......
回复 使用道具 举报
filter 中级黑马 2012-11-23 23:31:06
11#
种生祥 发表于 2012-11-23 23:04
就这样,文件夹跟盘符

/**
把C盘中所有目录里的JPG文件拷贝到当前目录下

*/

import java.io.*;
class CCopyToD2
{
        
        public static void main(String[] args) throws IOException
        {
                File dir = new File("f:\\abc");
               
                copyPic(dir);
        }
        
        public static void copyPic(File dir) throws IOException
        {
                File[] files = dir.listFiles();
                                if (files!=null){
                                         for(File f : files)
                                        {
                                                        if(f.isDirectory())
                                                                        copyPic(f);
                                                        else
                                                                if(f.getName().endsWith(".jpg"))
                                                                {
                                                                                       
                                                                        BufferedInputStream fi = new BufferedInputStream(new FileInputStream(f.getAbsolutePath()));
                                                                        BufferedOutputStream fo = new BufferedOutputStream(new FileOutputStream(f.getName()));
                                                                                       
                                                                        int by = 0;
                                                                        while((by=fi.read())!=-1)
                                                                                        fo.write(by);

                                                                        fo.close();
                                                                        fi.close();
                                                                        }
                                        }
                                }

        }
}
我加上了判断,测试文件夹和盘符都没问题啊
回复 使用道具 举报
filter 中级黑马 2012-11-23 23:31:49
12#
妈的,这代码贴上去怎么总是这么乱
回复 使用道具 举报
李志阳 发表于 2012-11-23 23:31
妈的,这代码贴上去怎么总是这么乱

对,加了判断两个都没问题,不加的话就出现我说的那种情况
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马