- import?java.io.*;??
- class?DownThread?extends?Thread?{??
- ????//定義字節數組(取水的竹筒)的長度??
- ????private?final?int?BUFF_LEN?=?32;??
- ????//定義讀取的起始點??
- ????private?long?start;??
- ????//定義讀取的結束點??
- ????private?long?end;??
- ????//讀取文件對應的輸入流??
- ????private?InputStream?is;??
- ????//將讀取到的字節輸出到raf中??
- ????private?RandomAccessFile?raf;??
- ??
- ????//構造器,傳入輸入流,輸出流和讀取起始點、結束點??
- ????public?DownThread(long?start,?long?end,?InputStream?is,?RandomAccessFile?raf)?{??
- ????????//輸出該線程負責讀取的字節位置??
- ????????System.out.println(start?+?"---->"?+?end);??
- ????????this.start?=?start;??
- ????????this.end?=?end;??
- ????????this.is?=?is;??
- ????????this.raf?=?raf;??
- ????}??
- ??
- ????public?void?run()?{??
- ????????try?{??
- ????????????is.skip(start);??
- ????????????raf.seek(start);??
- ????????????//定義讀取輸入流內容的的緩存數組(竹筒)??
- ????????????byte[]?buff?=?new?byte[BUFF_LEN];??
- ????????????//本線程負責讀取文件的大小??
- ????????????long?contentLen?=?end?-?start;??
- ????????????//定義最多需要讀取幾次就可以完成本線程的讀取??
- ????????????long?times?=?contentLen?/?BUFF_LEN?+?4;??
- ????????????//實際讀取的字節數??
- ????????????int?hasRead?=?0;??
- ????????????for?(int?i?=?0;?i?<?times;?i++)?{??
- ????????????????hasRead?=?is.read(buff);??
- ????????????????//如果讀取的字節數小于0,則退出循環!??
- ????????????????if?(hasRead?<?0)?{??
- ????????????????????break;??
- ????????????????}??
- ????????????????raf.write(buff,?0,?hasRead);??
- ????????????}??
- ????????}?catch?(Exception?ex)?{??
- ????????????ex.printStackTrace();??
- ????????}??
- ????????//使用finally塊來關閉當前線程的輸入流、輸出流??
- ????????finally?{??
- ????????????try?{??
- ????????????????if?(is?!=?null)?{??
- ????????????????????is.close();??
- ????????????????}??
- ????????????????if?(raf?!=?null)?{??
- ????????????????????raf.close();??
- ????????????????}??
- ????????????}?catch?(Exception?ex)?{??
- ????????????????ex.printStackTrace();??
- ????????????}??
- ????????}??
- ????}??
- }??
- ??
- public?class?MutilDown?{??
- ????public?static?void?main(String[]?args)?{??
- ????????final?int?DOWN_THREAD_NUM?=?4;??
- ????????final?String?OUT_FILE_NAME?=?"d:/copy勇敢的心.rmvb";??
- ????????InputStream[]?isArr?=?new?InputStream[DOWN_THREAD_NUM];??
- ????????RandomAccessFile[]?outArr?=?new?RandomAccessFile[DOWN_THREAD_NUM];??
- ????????try?{??
- ??
- ????????????isArr[0]?=?new?FileInputStream("d:/勇敢的心.rmvb");??
- ????????????long?fileLen?=?getFileLength(new?File("d:/勇敢的心.rmvb"));??
- ????????????System.out.println("文件的大小"?+?fileLen);??
- ????????????//以輸出文件名創建第一個RandomAccessFile輸出流??
- ????????????outArr[0]?=?new?RandomAccessFile(OUT_FILE_NAME,?"rw");??
- ????????????//創建一個與文件相同大小的空文件??
- ????????????for?(int?i?=?0;?i?<?fileLen;?i++)?{??
- ????????????????outArr[0].write(0);??
- ????????????}??
- ????????????//每線程應該讀取的字節數??
- ????????????long?numPerThred?=?fileLen?/?DOWN_THREAD_NUM;??
- ????????????//整個文件整除后剩下的余數??
- ????????????long?left?=?fileLen?%?DOWN_THREAD_NUM;??
- ????????????for?(int?i?=?0;?i?<?DOWN_THREAD_NUM;?i++)?{??
- ????????????????//為每個線程打開一個輸入流、一個RandomAccessFile對象,??
- ????????????????//讓每個線程分別負責讀取文件的不同部分。??
- ????????????????if?(i?!=?0)?{??
- ??
- ????????????????????isArr[i]?=?new?FileInputStream("d:/勇敢的心.rmvb");??
- ????????????????????//以指定輸出文件創建多個RandomAccessFile對象??
- ????????????????????outArr[i]?=?new?RandomAccessFile(OUT_FILE_NAME,?"rw");??
- ????????????????}??
- ????????????????if?(i?==?DOWN_THREAD_NUM?-?1)?{??
- ????????????????????//最后一個線程讀取指定numPerThred+left個字節??
- ????????????????????new?DownThread(i?*?numPerThred,?(i?+?1)?*?numPerThred??
- ????????????????????????????+?left,?isArr[i],?outArr[i]).start();??
- ????????????????}?else?{??
- ????????????????????//每個線程負責讀取一定的numPerThred個字節??
- ????????????????????new?DownThread(i?*?numPerThred,?(i?+?1)?*?numPerThred,??
- ????????????????????????????isArr[i],?outArr[i]).start();??
- ????????????????}??
- ????????????}??
- ????????}?catch?(Exception?ex)?{??
- ????????????ex.printStackTrace();??
- ????????}??
- ????}??
- ??
- ????public?static?long?getFileLength(File?file)?{??
- ????????long?length?=?0;??
- ????????//獲取文件的長度??
- ????????long?size?=?file.length();??
- ????????length?=?size;??
- ????????return?length;??
- ????}??
- }?