本帖最后由 nailsoul 于 2012-5-13 16:24 编辑
RandomAccessFile 里面应该没有定义缓冲区数组吧? 我在那个类里面没看到数组成员变量 如果有的话 如果一个文件足够大的话 全部存储在byte数组中会发生堆内存溢出啊 还有用该类时我不用数当缓冲区 复制一个大文件时慢的跟乌龟爬似的 还有我下面的代码为什么复制完文件以后 播放不了啊 ? 复制后的文件跟复制前的文件一样大的 啊- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.RandomAccessFile;
- import java.util.Collections;
- public class RandomAccessInputSreamDemo {
- public static void main(String[] args) throws IOException {
- String s=new String();
- CopyFile cf1=new CopyFile(s);
- CopyFile cf2=new CopyFile(s);
- CopyFile cf3=new CopyFile(s);
- CopyFile cf4=new CopyFile(s);
- cf1.start();
- cf2.start();
- cf3.start();
- cf4.start();
- }
- }
- class CopyFile extends Thread{
- private static int count =0;
- private String lock;
- public CopyFile(String lock){
- this.lock=lock;
- }
- @Override
- public void run(){
- RandomAccessFile readRaf=null;
- RandomAccessFile raf=null;
- try {
- readRaf=new RandomAccessFile("c:/张孝祥JAVA基础2_1.avi","r");
- raf=new RandomAccessFile("d:/张孝祥JAVA基础2_1.avi","rw");
- long fileLength=readRaf.length();
- long tmpLen=fileLength/4;
- long copyLength=0;
- synchronized(lock){
- copyLength=count==3?fileLength-1:(tmpLen+1)*count-1;
- raf.seek(count*tmpLen);
- readRaf.seek(count++*tmpLen);
- }
- int number=(int)(copyLength/(1024*1024*3));
- int tmp=(int)(copyLength-number*(1024*1024*3));
- byte buf[]=new byte[1024*1024*3];
- while(readRaf.getFilePointer()<=copyLength){
- if(number--==1){
- buf=new byte[(1024*1024*3)+tmp];
- }
- int b=readRaf.read(buf);
- raf.write(buf,0,b);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- try {
- readRaf.close();
- raf.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
复制代码 |
|