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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 霍振鹏 中级黑马   /  2014-4-16 16:15  /  1413 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 霍振鹏 于 2014-4-16 16:17 编辑

想利用多线程,完成复制一个文件的功能,但是,复制后的文件大小与源文件大小不一致。不知道哪儿出错了?
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.io.RandomAccessFile;

  5. public class RandomAccessFileDownLoad {

  6.         /**
  7.          * @param args
  8.          * @throws FileNotFoundException
  9.          */
  10.         public static void main(String[] args) throws FileNotFoundException {
  11.                 // TODO Auto-generated method stub
  12.                 File file = new File("a.txt");
  13.                 final RandomAccessFile reader = new RandomAccessFile(file, "r");
  14.                 final RandomAccessFile writer = new RandomAccessFile("b.txt", "rw");
  15.                 long file_length = file.length();
  16.                 System.out.println(file_length);
  17.                 final long block = (long) Math.ceil(file_length / 5);
  18.                 for (int i = 0; i < 5; i++) {
  19.                         final int temp_i = i;
  20.                         new Thread(new Runnable() {

  21.                                 @Override
  22.                                 public void run() {
  23.                                         // TODO Auto-generated method stub
  24.                                         long start = temp_i * block;
  25.                                         long end = (temp_i + 1) * block - 1;
  26.                                         try {
  27.                                                 reader.seek(start);
  28.                                                 writer.seek(start);
  29.                                                 System.out.println(Thread.currentThread().getName()
  30.                                                                 + "     " + start);
  31.                                                 byte[] bytearr = new byte[1024];
  32.                                                 int length = 0;
  33.                                                 while ((length = reader.read(bytearr)) != -1) {
  34.                                                         writer.write(bytearr, 0, length);

  35.                                                 }

  36.                                         } catch (IOException e) {
  37.                                                 // TODO Auto-generated catch block
  38.                                                 e.printStackTrace();
  39.                                         }

  40.                                 }

  41.                         }).start();

  42.                 }
  43.         }

  44. }
复制代码




看运行结果,各个线程处理的范围应该是每错的


1234566.png (9.49 KB, 下载次数: 11)

1234566.png

评分

参与人数 1技术分 +1 收起 理由
SyouRai_Tsk + 1

查看全部评分

6 个回复

倒序浏览
此问题是没有刷新的缘故 write.flush();
回复 使用道具 举报
SyouRai_Tsk 发表于 2014-4-16 16:35
此问题是没有刷新的缘故 write.flush();

可是我查了一下,它并不能调用flush方法,它自己本身没有flush方法,它的父类和他实现的接口中也没有此方法。
回复 使用道具 举报
霍振鹏 发表于 2014-4-16 16:45
可是我查了一下,它并不能调用flush方法,它自己本身没有flush方法,它的父类和他实现的接口中也没有此方 ...

我无知了!!!
回复 使用道具 举报

呵呵 互相学习呗!
回复 使用道具 举报
  1. /*
  2. RandomAccessFile 类
  3. getFilePointer();//获取指针
  4. skipBytes();//跳过字节
  5. seek(); 设置指针

  6. 实现多线程分段输出
  7. */
  8. import java.io.*;
  9. class Demo3
  10. {
  11.         public static void main(String[] args)throws Exception
  12.         {
  13.                 //读取
  14.                 RandomAccessFile raf=new RandomAccessFile("g.mp3","rw");
  15.                 //写入
  16.                 RandomAccessFile raf1=new RandomAccessFile("gg.mp3","rw");
  17.                 //三个线程进行分段写入
  18.                 //seek 设置指针位置 实现分段功能
  19.                 new Thread(new Thread1(raf,raf1,0)).start();

  20.                 new Thread(new Thread1(raf,raf1,2097152)).start();
  21.                
  22.                 new Thread(new Thread1(raf,raf1,4194304)).start();
  23.         }
  24. }
  25. //多线程
  26. class Thread1 implements Runnable
  27. {
  28.         private RandomAccessFile raf;
  29.         private RandomAccessFile raf1;
  30.         private int skip;
  31.         public Thread1(RandomAccessFile raf,RandomAccessFile raf1,int skip){
  32.                 this.raf=raf;
  33.                 this.raf1=raf1;
  34.                 this.skip=skip;
  35.         }
  36.         //run方法
  37.         public void run(){
  38.            try
  39.            {
  40.                    byte []buf=new byte[1024*1024*2];
  41.                    int len=0;
  42.                         len=raf.read(buf);
  43.                         raf1.write(buf,0,len);
  44.            }
  45.            catch (IOException e)
  46.            {
  47.                    System.out.println(e.getMessage());
  48.            }
  49.            finally
  50.            {
  51.                         try
  52.                         {
  53.                                 if(raf!=null)
  54.                                         raf.close();
  55.                                 if(raf1!=null)
  56.                                         raf1.close();
  57.                         }
  58.                         catch (IOException e)
  59.                    {
  60.                            System.out.println(e.getMessage());
  61.                    }
  62.            }
  63.         }
  64. }
复制代码


我这个貌似很彩笔
回复 使用道具 举报
ノtrack 发表于 2014-4-17 21:13
我这个貌似很彩笔

你这种不能实现具体的自动分配拷贝啊 = ={:3_49:}
是手动分配的具体哪个线程从哪部分拷贝到哪部分。。。

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