当前位置: 首页 > 程序语言 >java中文件复制和计数问题

java中文件复制和计数问题

作者 好吃的苦瓜
来源: 小木虫 250 5 举报帖子
+关注

哪位大神能帮我看看下面的代码出现什么问题了吗
代码的目的是要复制G:\\java\\ddd\\ccc文件夹下的.txt文件到G:\\java\\ddd\\aaa文件夹中,并计算.txt文件的数量,但是我运行以后有3个.txt文件但是count的返回值却是2,不知道是哪里错了,其他地方出现问题也请帮我指出来,感谢

CODE:
import java.io.File;
public class Test02 {

        public static void main(String[] args) throws IOException {
                File file1=new File("G:\\java\\ddd\\ccc"
                File file2=new File("G:\\java\\ddd\\aaa"
                int count=0;
                System.out.println("txt文件的个数为:"+getCount(file1,file2,count));;
        }
       
        public static int getCount(File file1,File file2,int count) throws IOException{
                if(!file1.exists()){
                        System.out.println("被复制文件不存在!"
                        return count;
                }
                if(!file2.exists()){
                        System.out.println("复制文件不存在!"
                        return count;       
                }
                File[] f=file1.listFiles();
                for(File fi:f){
                        if(fi.isDirectory()){
                                getCount(fi,file2,count);
                        }else if(fi.isFile()){
                                if(fi.getName().endsWith(".txt&quot){
                                        ++count;
                                        File file=new File(file2,fi.getName());
                                        FileInputStream fis=new FileInputStream(fi);
                                        FileOutputStream fos=new FileOutputStream(file);
                               
                                        int len=0;
                                        byte[] b=new byte[1024];
                                        while((len=fis.read(b))!=-1){
                                                fos.write(b, 0, len);
                                        }
                                       
                                        fis.close();
                                        fos.close();
                                       
                                }
                        }
                       
                }
                return count;
        }

}

[ Last edited by jjdg on 2017-11-5 at 23:46 ] 返回小木虫查看更多

今日热帖
  • 精华评论
  • 好吃的苦瓜

    这里面的表情本来是右括号,发布出来全成了表情

  • iamluo

    没用过Java,勉强掺和一下吧。1文件隐没隐藏?试着看下属性。2支持汉字文件名吗?用不用选汉字编码。3java的整型是从0开始的还是从1开始的?

  • curehabit

    fi.isDirectory() 调用getCount并不影响最终返回结果

猜你喜欢