多线程文件复制(界面)1

package netWorkMoreThreadCopy;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.RandomAccessFile;

public class CopyFile implements Runnable {
//来源文件
private String sourceFileName;
//目标文件
private String targetFileName;
//分块总数
private int blockCount;
//开始COPY的块序号
private int blockNo;
//缓存大小
private int maxBuffSize=1024*1024;
/**
* 将sourceFileName文件分blockCount块后的第blockNo块复制至sourceFileName
* @param sourceFileName 来源文件全名
* @param targetFileName 目标文件全名
* @param blockCount 文件分块COPY数
* @param blockNo 开始COPY的块序号
*/
public CopyFile(String sourceFileName,String targetFileName,int blockCount,int blockNo)
{
this.sourceFileName=sourceFileName;
this.targetFileName=targetFileName;
this.blockCount=blockCount;
this.blockNo=blockNo;
}
public void run() {
//得到来源文件
File file=new File(sourceFileName);
//得到来源文件的大小
long size=file.length();
//根据文件大小及分块总数算出单个块的大小
long blockLenth=size/blockCount;
//算出当前开始COPY的位置
long startPosition=blockLenth*blockNo;
//实例化缓存
byte[] buff=new byte[maxBuffSize];
try{
//从源文件得到输入流
InputStream inputStream=new FileInputStream(sourceFileName);
//得到目标文件的随机访问对象
RandomAccessFile raf=new RandomAccessFile(targetFileName,"rw");
//将目标文件的指针偏移至开始位置
raf.seek(startPosition);
//当前读取的字节数
int curRedLength;
//累计读取字节数的和
int totalRedLength=0;
//将来源文件的指针偏移至开始位置
inputStream.skip(startPosition);
//依次分块读取文件
while((curRedLength=inputStream.read(buff))>0 && totalRedLength<blockLenth)
{
//将缓存中的字节写入文件?目标文件中
raf.write(buff, 0, curRedLength);
//累计读取的字节数
totalRedLength+=curRedLength;
}
//关闭相关资源
raf.close();
inputStream.close();
}catch(Exception ex)
{
ex.printStackTrace();
}
}
}
package netWorkMoreThreadCopy;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SwingForFun extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private Test test;
JPanel jp;
JTextField sourceFileName;
JTextField targetFileName;
JTextField blockCount; //分块数
JButton startCopy;
JTextField endTime;//显示所用的时间

JButton jb1=new JButton("浏览");
JButton jb2=new JButton("浏览");

public void setEndTime(JTextField endTime) {
this.endTime = endTime;
}
public JTextField getEndTime() {
return endTime;
}
int bb;

/*public static void main(String[] args) throws Exception {
new SwingForFun();
}*/

SwingForFun(Test test) throws Exception {
this.test=test;

this.setTitle("复制界面");
jp = new JPanel();
jp.setLayout(new GridLayout(4, 3,5,5));
addComponents(); //初始化面板!
this.setLayout(new BorderLayout());
this.add(new JLabel(" "), BorderLayout.NORTH);
this.add(new JLabel(" "), BorderLayout.SOUTH);
this.add(new JLabel(" "), BorderLayout.WEST);
this.add(new JLabel(" "), BorderLayout.EAST);
this.add(jp);
this.setVisible(true);
this.setLocation(300,300);
this.setSize(500, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void addComponents() {
JLabel jbsourceFile = new JLabel("源文件路径:", JLabel.CENTER);
JLabel jbtargetFile = new JLabel("目标文件路径:", JLabel.CENTER);
JLabel jbEndTime = new JLabel("进程数量:", JLabel.CENTER);
sourceFileName = new JTextField();
targetFileName = new JTextField();
blockCount=new JTextField();

endTime=new JTextField("共用时:__________");
endTime.setEditable(false);

/*jb1.setPreferredSize(new Dimension(20,20));
jb2.setPreferredSize(new Dimension(20,20));*/

startCopy = new JButton("开始复制");
jp.add(jbsourceFile);
jp.add(sourceFileName);
jp.add(jb1);
jp.add(jbtargetFile);
jp.add(targetFileName);
jp.add(jb2);
jp.add(jbEndTime);
jp.add(blockCount);
jp.add(new JLabel(""));

jp.add(startCopy);
jp.add(endTime);

jb1.addActionListener(this);
jb2.addActionListener(this);
startCopy.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb1){
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
sourceFileName.setText(chooser.getSelectedFile().getAbsolutePath());
}
}
if(e.getSource()==jb2){
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
targetFileName.setText(chooser.getSelectedFile().getAbsolutePath());
}
}

if (e.getSource() == startCopy) {
String sc = sourceFileName.getText().trim();
//返回字符串的副本,忽略前导空白和尾部空白
String tg =targetFileName.getText().trim();
//需对错误的路径填写进行判断。
System.out.println(sc+tg);
if(sc.compareTo("")==0||tg.compareTo("")==0||blockCount.getText().compareTo("")==0){
System.out.println("dd");
}else{
int num=Integer.parseInt(blockCount.getText());
test.getValues(sc, tg, num);
}
}
}
}
package netWorkMoreThreadCopy;
public class Test {
//来源文件
private static String sourceFile;
//目标文件
private static String targetFile;
//分块数
private static int blockCount;
//界面
private static SwingForFun sff;

public static void main(String[] args) {
Test t=new Test();
try {
sff=new SwingForFun(t);
} catch (Exception e1) {
e1.printStackTrace();
}
}
//取到界面中所填的值
public void getValues(String sc,String tg,int m){
sourceFile=sc;
targetFile=tg;
blockCount=m;

//记录开始时间
long beginTime=System.currentTimeMillis();
//依次分块进行文件COPY
for(int i=0;i<blockCount;i++)
{
//实例化文件复制对象
CopyFile copyFile=new CopyFile(sourceFile,targetFile,blockCount,i);
//实例化线程
Thread thread=new Thread(copyFile);
//开始线程
thread.start();
try
{
//加入线程
thread.join();
}
catch (Exception e) {
e.printStackTrace();
}
}
//计算耗时
long endTime=System.currentTimeMillis();
//输出耗时
//System.out.println("共用时:"+(endTime-beginTime)+"ms");
String str="共用时:"+(endTime-beginTime)+"ms";
sff.getEndTime().setText(str);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值