JAVA多线程复制文件

关于JAVA多线程复制文件的一次尝试

主线程:
package  org.ota.action;

import  java.io.File;

/**
 *
 *
 * 

application name:


 * 

application describing:多线程读文件和写文件


 * 


 * 

company:neusoft


 * 

time:Nov 24, 2007


 * 
@author OTA
 * 
@version $Revision
 
*/
 
public   class  HtmlPraser  extends  Thread
{   
    
boolean stop = false;
    
    SubThread[] sub 
= new SubThread[5];
    
    
public void run()
    
{
        File file 
= new File("d:/a.txt");
            
for(int i=0;i<5;i++)
            
{
                sub[i] 
= new SubThread("D:/temp.txt",i,file.length());
                sub[i].start();
            }

            
while(!stop)
            
{
                
try
                
{
                    Thread.sleep(
500);
                }

                
catch (InterruptedException e)
                
{
                    e.printStackTrace();
                }

                
for(int i=0;i<5;i++)
                
{
                    stop 
= true;
                    
if(sub[i].done == false)
                    
{
                        stop 
= false;
                        
break;
                    }

                }

            }

            System.out.println(
"文件传输完毕");
    }

    
    
public static void main(String[] args)
    
{
        HtmlPraser html 
= new HtmlPraser();
        html.start();
    }


}

子线程:
package  org.ota.action;

import  java.io.FileNotFoundException;
import  java.io.IOException;
import  java.io.RandomAccessFile;

public   class  SubThread  extends  Thread
{
    RandomAccessFile oSavedFile;
    
    RandomAccessFile oFromFile;
    
    String threadID;
    
    
long length;
    
    
int start;
    
    
int end;
    
    
int n;
    
    
boolean done = false;
    
    
/**
     * 构造器方法,找到此线程的文件指针
     * 
     * 
@param path
     * 
@param i
     * 
@param length
     * 
@see
     * 
@author: OTA
     
*/

    
public SubThread(final String path,int i,long length)
    
{
        
try
        
{
            oSavedFile 
= new RandomAccessFile(path, "rw");
            oFromFile 
= new RandomAccessFile("d:/a.txt","rw");
            
this.length = length;    
            start 
= (int) (i*(length/5));
            end 
= (int)((i+1)*(length/5));
            oSavedFile.seek(start);
//定位到START的位置进行下载
            oFromFile.seek(start);
            
this.threadID = ""+i+"线程";
            System.out.println(start);
        }

        
catch (FileNotFoundException e)
        
{
            e.printStackTrace();
        }

        
catch (IOException e)
        
{
            e.printStackTrace();
        }

    }

    
    
public void run()
    
{
        
try
        
{
            
byte[] buf = new byte[1024]; 
            
while(start<end&&start!=-1)
            
{
                Thread.sleep(
1);
                start 
+= oFromFile.read(buf, 01024);
                oSavedFile.write(buf,
0,1024);
                System.out.println(
"写入"+1024+"字节到"+this.threadID);
            }

            done 
= true;
            System.out.println(
this.threadID+"结束");
        }

        
catch (FileNotFoundException e)
        
{
            e.printStackTrace();
        }

        
catch (IOException e)
        
{
            e.printStackTrace();
        }

        
catch (InterruptedException e)
        
{
            e.printStackTrace();
        }

        
    }


}

你可能感兴趣的:(JAVA多线程复制文件)