import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.BoxView;
public class CopyFileDemo extends JFrame{
JFileChooser fileChooser;
JTextField txtSource;
JTextField textDest;
JButton btnSelectA;
JButton btnSelectB;
JButton btncopy;
public CopyFileDemo(){
super("Java cpyfile");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fileChooser=new JFileChooser();
txtSource=new JTextField(16);
textDest=new JTextField(16);
btnSelectA=new JButton("选择");
btnSelectB=new JButton("选择");
btncopy=new JButton("复制");
Box box=Box.createVerticalBox();
JPanel panel=new JPanel();
panel.add(new JLabel("源文件"));
panel.add(txtSource);panel.add(btnSelectA);
box.add(panel);
panel=new JPanel();
panel.add(new JLabel("目标文件"));
panel.add(textDest);panel.add(btnSelectB);
box.add(panel);
box.add(btncopy);
getContentPane().add("Center",box);
}//
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==btncopy)
{
String sourceFile=txtSource.getText();
String targetFile=textDest.getText();
if(copyFile(sourceFile, targetFile))
{
JOptionPane.showMessageDialog(CopyFileDemo.this, "Succeed!");
}
else {
JOptionPane.showMessageDialog(CopyFileDemo.this, "Failure!");
}
}
else {
if(fileChooser.showOpenDialog(CopyFileDemo.this)==JFileChooser.APPROVE_OPTION)
{
String fileName=fileChooser.getSelectedFile().getAbsolutePath();
if(event.getSource().equals(btnSelectA))
txtSource.setText(fileName);
else
textDest.setText(fileName);
}
}
}
}////
public boolean copyFile(String file1,String file2)
{
try {
File fileIn=new File(file1);
File fileOut=new File(file2);
FileInputStream fInputStream=new FileInputStream(fileIn);
FileOutputStream fOutputStream=new FileOutputStream(fileOut);
byte[] bytes=new byte[1024];
int c;
while ((c=fInputStream.read(bytes))!=-1) {
fOutputStream.write(bytes,0,c);
}
fInputStream.close();
fOutputStream.close();
return true;
} catch (Exception e) {
// TODO: handle exception
return false;
}
}
public static void main(String args[])
{
CopyFileDemo obj=new CopyFileDemo();
}
}
这个java 为什么 会出现