文件复制
package my;
import java.io.File;
public class Test
{
public static void main(String[] args)
{
File srcFile = new File("/Users/ding/Desktop/11.zip");
File dstFile = new File("/Users/ding/Desktop/2.zip");
try
{
MyFileUtils.copyFile(srcFile, dstFile);
} catch (Exception e)
{
e.printStackTrace();
}
System.out.println("完成");
}
}
package my;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class MyFileUtils
{
public static void copyFile(File srcFile, File dstFile) throws Exception
{
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = new FileInputStream(srcFile);
outputStream = new FileOutputStream(dstFile);
byte[] buf = new byte[8192];
while(true)
{
int n = inputStream.read(buf);
if(n <= 0) break;
outputStream.write(buf, 0, n);
}
}
finally {
try { inputStream.close();}catch(Exception e) {}
try { outputStream.close();} catch(Exception e) {}
}
}
}
短任务

package my;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Swing2
{
private static void createGUI()
{
JFrame frame = new MyFrame("Swing Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 300);
frame.setVisible(true);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run()
{
createGUI();
}
});
}
}
package my;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
public class MyFrame extends JFrame
{
JButton okButton = new JButton("开始");
WaitDialog waitDialog = null;
public MyFrame(String title)
{
super(title);
JPanel root = new JPanel();
this.setContentPane(root);
root.setLayout(new FlowLayout());
root.add(okButton);
okButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e)
{
onMouseClicked();
}
});
}
private void onMouseClicked()
{
File srcFile = new File("/Users/ding/Desktop/11.zip");
File dstFile = new File("/Users/ding/Desktop/22.zip");
ShortTask th = new ShortTask();
th.execute(srcFile, dstFile);
waitDialog = new WaitDialog(this);
waitDialog.exec();
}
private class ShortTask extends Thread
{
private File srcFile, dstFile;
public void execute(File srcFile, File dstFile)
{
this.srcFile = srcFile;
this.dstFile = dstFile;
start();
}
@Override
public void run()
{
try {
doInBackground();
}catch(Exception e)
{
e.printStackTrace();
}
try { sleep(100); }catch(Exception e) {}
SwingUtilities.invokeLater( ()->{
done();
});
}
protected void doInBackground() throws Exception
{
MyFileUtils.copyFile(srcFile, dstFile);
}
protected void done()
{
if(waitDialog!=null)
{
waitDialog.setVisible(false);
waitDialog = null;
}
}
}
}
package my;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class MyFileUtils
{
public static void copyFile(File srcFile, File dstFile) throws Exception
{
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = new FileInputStream(srcFile);
outputStream = new FileOutputStream(dstFile);
byte[] buf = new byte[8192];
while(true)
{
int n = inputStream.read(buf);
if(n <= 0) break;
outputStream.write(buf, 0, n);
}
}
finally {
try { inputStream.close();}catch(Exception e) {}
try { outputStream.close();} catch(Exception e) {}
}
}
}
package my;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;
public class WaitDialog extends JDialog
{
public JLabel display = new JLabel("请等待...");
public WaitDialog(JFrame owner)
{
super(owner, "", true);
this.setUndecorated(true);
JPanel root = new JPanel();
this.setContentPane(root);
root.setLayout(new BorderLayout());
root.setOpaque(true);
root.setBackground(new Color(0xF4F4F4));
root.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
display.setHorizontalAlignment(SwingConstants.CENTER);
root.add(display, BorderLayout.CENTER);
this.setSize(200, 80);
}
public void exec()
{
Rectangle frmRect = this.getOwner().getBounds();
int width = this.getWidth();
int height = this.getHeight();
int x = frmRect.x + (frmRect.width - width) / 2;
int y = frmRect.y + (frmRect.height - height) / 2;
this.setBounds(x, y, width, height);
this.setVisible(true);
}
}
Af封装

package my;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Swing2
{
private static void createGUI()
{
JFrame frame = new MyFrame("Swing Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 300);
frame.setVisible(true);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run()
{
createGUI();
}
});
}
}
package my;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import af.swing.thread.AfShortTask;
public class MyFrame extends JFrame
{
JButton okButton = new JButton("开始");
WaitDialog waitDialog = null;
public MyFrame(String title)
{
super(title);
JPanel root = new JPanel();
this.setContentPane(root);
root.setLayout(new FlowLayout());
root.add(okButton);
okButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e)
{
onMouseClicked();
}
});
}
private void onMouseClicked()
{
File srcFile = new File("/Users/ding/Desktop/11.zip");
File dstFile = new File("/Users/ding/Desktop/33.zip");
CopyTask th = new CopyTask();
th.execute(srcFile, dstFile);
waitDialog = new WaitDialog(this);
waitDialog.exec();
}
private class CopyTask extends AfShortTask
{
@Override
protected void doInBackground() throws Exception
{
File srcFile = (File)this.args[0];
File dstFile = (File)this.args[1];
MyFileUtils.copyFile(srcFile, dstFile);
}
@Override
protected void done()
{
if(waitDialog!=null)
{
waitDialog.setVisible(false);
waitDialog = null;
}
}
}
}
package my;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class MyFileUtils
{
public static void copyFile(File srcFile, File dstFile) throws Exception
{
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = new FileInputStream(srcFile);
outputStream = new FileOutputStream(dstFile);
byte[] buf = new byte[8192];
while(true)
{
int n = inputStream.read(buf);
if(n <= 0) break;
outputStream.write(buf, 0, n);
}
}
finally {
try { inputStream.close();}catch(Exception e) {}
try { outputStream.close();} catch(Exception e) {}
}
}
}
package my;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;
public class WaitDialog extends JDialog
{
public JLabel display = new JLabel("请等待...");
public WaitDialog(JFrame owner)
{
super(owner, "", true);
this.setUndecorated(true);
JPanel root = new JPanel();
this.setContentPane(root);
root.setLayout(new BorderLayout());
root.setOpaque(true);
root.setBackground(new Color(0xF4F4F4));
root.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
display.setHorizontalAlignment(SwingConstants.CENTER);
root.add(display, BorderLayout.CENTER);
this.setSize(200, 80);
}
public void exec()
{
Rectangle frmRect = this.getOwner().getBounds();
int width = this.getWidth();
int height = this.getHeight();
int x = frmRect.x + (frmRect.width - width) / 2;
int y = frmRect.y + (frmRect.height - height) / 2;
this.setBounds(x, y, width, height);
this.setVisible(true);
}
}
package af.swing.thread;
import javax.swing.SwingUtilities;
public abstract class AfShortTask extends Thread
{
public Object[] args;
public Exception err;
public void execute(Object...args)
{
this.args = args;
start();
}
@Override
public void run()
{
try
{
doInBackground();
} catch (Exception e)
{
this.err = e;
System.out.println("** AfShortTask: " + e.getMessage());
}
try
{
sleep(100);
} catch (Exception e)
{
}
SwingUtilities.invokeLater(() -> {
done( );
});
}
protected abstract void doInBackground() throws Exception;
protected abstract void done();
}
SwingWorker封装
package my;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class MyFrame extends JFrame
{
JButton okButton = new JButton("开始");
WaitDialog waitDialog = null;
public MyFrame(String title)
{
super(title);
JPanel root = new JPanel();
this.setContentPane(root);
root.setLayout(new FlowLayout());
root.add(okButton);
okButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e)
{
onMouseClicked();
}
});
}
private void onMouseClicked()
{
File srcFile = new File("c:/example/a1.rar");
File dstFile = new File("c:/example/a2.rar");
CopyTask th = new CopyTask(srcFile, dstFile);
th.execute();
waitDialog = new WaitDialog(this);
waitDialog.exec();
}
private class CopyTask extends SwingWorker
{
File srcFile, dstFile;
public CopyTask(File srcFile, File dstFile)
{
this.srcFile = srcFile;
this.dstFile = dstFile;
}
@Override
protected Object doInBackground() throws Exception
{
MyFileUtils.copyFile(srcFile, dstFile);
return null;
}
@Override
protected void done()
{
if(waitDialog!=null)
{
waitDialog.setVisible(false);
waitDialog = null;
}
}
}
}