当前位置: 代码迷 >> Java Web开发 >> 文件的复制
  详细解决方案

文件的复制

热度:251   发布时间:2007-07-12 21:25:58.0
文件的复制

程序代码:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class FrmMain extends Applet implements Runnable, ActionListener
{
private TextArea msg;
private JTextField t;
private JButton b1;
private JButton b2;
private JPanel p;

private FileInputStream fin=null;
private FileOutputStream fout=null;

private String pathname=null;
private String filename=null;
private byte buf[];

Thread th;

public void init()
{
msg=new TextArea(50,20);
t=new JTextField(20);
b1=new JButton(\"选择\");
b2=new JButton(\"复制\");
b1.setFont(new Font(\"DialogInput\", 0, 12));
b2.setFont(new Font(\"DialogInput\", 0, 12));
p=new JPanel();
this.setLayout(new BorderLayout());
this.add(\"Center\",msg);
this.add(\"South\",p);
p.add(t);
p.add(b1);
p.add(b2);

b1.addActionListener(this);
b2.addActionListener(this);
}
public void run()
{
try
{
fin=new FileInputStream(pathname);
fout=new FileOutputStream(filename);
buf=new byte[1024];
msg.append(\"正在复制文件\"+filename+\"\n\");
while(fin.read(buf)!=-1)
{
fout.write(buf);
}
msg.append(\"复制成功!\n\");
}
catch(Exception e)
{
System.out.println(e);
msg.append(\"复制失败!\n\");
}

}
public static void main(String[] args)
{
FrmMain app=new FrmMain();
Frame frm=new Frame(\"坏碟文件拷贝工具\");
app.init();
app.start();
frm.add(app);
frm.setSize(400,300);
frm.setResizable(false);
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
Dimension f=frm.getSize();
frm.setLocation(d.width/2-f.width/2,d.height/2-f.height/2);
frm.show();
frm.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
});
}

public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals(\"选择\"))
{
FileDialog fdlg=new FileDialog(new Frame(),\"选择传送的文件\",FileDialog.LOAD);
fdlg.setVisible(true);
t.setText(fdlg.getDirectory()+fdlg.getFile());
filename=fdlg.getFile();
pathname=t.getText();
}
else
{
if(pathname.equals(\"\")||pathname.equals(\"nullnull\"))
{
JOptionPane.showMessageDialog(null,\"没有选择发送的文件\",\"错误\",JOptionPane.ERROR_MESSAGE);
return;
}

th=new Thread(this);
th.start();
}
}
}


搜索更多相关主题的帖子: 文件  

----------------解决方案--------------------------------------------------------
多谢分享

[此贴子已经被作者于2007-7-12 22:58:57编辑过]



----------------解决方案--------------------------------------------------------

我也有一个:



package file;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyFile {

/**
* @param args
*/

public void copyFile(String src,String des){
FileInputStream fis=null;
FileOutputStream fos=null;
try {
fis = new FileInputStream(src);
fos = new FileOutputStream(des);
byte[ ]buffer = new byte[512];
int n=0;
while((n=fis.read(buffer))!=-1){
fos.write(buffer, 0, n);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
fis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CopyFile cf = new CopyFile();
cf.copyFile("F:/date.java", "d:/com/date.java");
}

}


----------------解决方案--------------------------------------------------------
  相关解决方案