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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© EDDY_Liang 中级黑马   /  2014-7-15 18:15  /  1385 人查看  /  11 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. public static void main(String[] args) throws IOException {
  2.                 File file = new File("f:\\abc.mkv");
  3.                 fileIntoPartMethod(file);
  4.                
  5.         }
  6. public static void fileIntoPartMethod(File file) throws IOException {
  7.                 FileInputStream fis = new FileInputStream(file);
  8.                 FileOutputStream fos = null;
  9.                 File dir = new File("f:\\fileParts");
  10.                 if(!dir.exists()){
  11.                         dir.mkdirs();
  12.                 }
  13.                 byte[] buf = new byte[1024*1024];
  14.                 int len = 0;
  15.                 while((len=fis.read(buf))!= -1){
  16.                         int count = 0;
  17.                         fos = new FileOutputStream(new File(dir,(count++)+".part"));
  18.                         fos.write(buf, 0, len);
  19.                         fos.flush();
  20.                        
  21.                 }
  22.                 fos.close();
  23.                 fis.close();
  24.         }

  25. }
复制代码

伙伴们知道为什么我运行了之后,碎片文件夹创建起来了,但文件夹里为空的?我看了视频感觉是一样的啊??

11 个回复

倒序浏览
程序没问题。唯一就是要把int count = 0 ; 放到循环之前。不然每次循环都为0,新文件始终是0.part名字会覆盖前面的。
回复 使用道具 举报
jwx555 发表于 2014-7-15 18:35
程序没问题。唯一就是要把int count = 0 ; 放到循环之前。不然每次循环都为0,新文件始终是0.part名字会覆 ...

谢谢兄弟,不过现在放在外面,怎么输出的文件名会从0.part开始??而不是从1.part开始输出呢??
回复 使用道具 举报
jwx555 发表于 2014-7-15 18:35
程序没问题。唯一就是要把int count = 0 ; 放到循环之前。不然每次循环都为0,新文件始终是0.part名字会覆 ...

还有,我不懂为什么count放在里面,每次循环的会文件名一样,会覆盖,但运行后连一个文件都没有,这个很难理解啊??怎么解释这种情况?
回复 使用道具 举报
我是来拿马币的
回复 使用道具 举报
File类型dir貌似没用,count要放在外面,否则每次读完1M后,都会再次创建0.part
回复 使用道具 举报
package Reflect;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class txet {
       
        public static void main(String[] args) throws IOException {
        File file = new File("f:\\Enjoy life.docx");
        fileIntoPartMethod(file);
        
}
public static void fileIntoPartMethod(File file) throws IOException {
        FileInputStream fis = new FileInputStream(file);
        FileOutputStream fos = null;
        File dir = new File("f:\\acb");
        if(!dir.exists()){
                dir.mkdirs();
        }
        byte[] buf = new byte[5120];
        int len = 0;
        int count = 0;
        while((len=fis.read(buf))!= -1){
               
                fos = new FileOutputStream(new File(dir,(++count)+".part"));
                fos.write(buf, 0, len);
                fos.flush();
                count++;
        }
        fos.close();
        fis.close();
}

}
我改你哈你的代码  你看看 应该可以   1024*1024  太大了    文件1024为1kb    乘个5都差不多了
回复 使用道具 举报
wisely 中级黑马 2014-7-15 20:52:23
8#
  1. fos=new FileOutputStream(new File(dir,(count++)+".part"));
复制代码

回复 使用道具 举报
wisely 中级黑马 2014-7-15 20:53:26
9#

谁能解释一下这句代码,new File只是建了个对象,而那个文件并不存在,竟然能够运行,为么?

点评

new File没有自动创建文件的功能吧。new FileWriter倒是可以。  发表于 2014-7-15 21:48
自动创建文件  发表于 2014-7-15 21:29
回复 使用道具 举报
路过 走走
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马